我已经成功地使用 spring-boot 2.5.4、camel 3.13.0 和 hibernate 5.4.32.Final 运行它。我不确定这是不是最好的方法,但是关于这个主题的文档很差。
这里没有提到创建sql表https://camel.apache.org/components/3.13.x/jpa-component.html#_spring_boot_auto_configuration
但在https://camel.apache.org/components/3.13.x/sql-component.html 上找到了一些有用的信息
Maven 依赖项
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jpa-starter</artifactId>
<version>3.13.0</version>
</dependency>
创建sql表和序列
CREATE TABLE CAMEL_MESSAGEPROCESSED
(
id number(38) primary key not null,
processor_name VARCHAR(255),
message_id VARCHAR(100),
created_at TIMESTAMP
);
create sequence hibernate_sequence start with 1 increment by 50;
Jpa 实体配置(spring bean)包含实体org.apache.camel.processor.idempotent.jpa.MessageProcessed
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
@EntityScan(basePackages = {
"project.base.package.to.do",
"org.apache.camel.processor.idempotent.jpa"
})
@Configuration
class JpaEntityConfiguration {
}
创建 JpaMessageIdRepository。处理器名称应更改。 EntityManagerFactory 是从 spring 上下文自动装配的。
private JpaMessageIdRepository createJpaMessageIdRepository(EntityManagerFactory entityManagerFactory) {
return JpaMessageIdRepository.jpaMessageIdRepository(entityManagerFactory, "helloSOProcessor");
}
覆盖 id 生成策略(仅当您使用带有名称的序列而不是其他 hibernate_sequence 并且不想创建 hibernate_sequence 时)。在 META-INF 下创建 orm.xml
<entity-mappings>
<entity class="org.apache.camel.processor.idempotent.jpa.MessageProcessed" name="MessageProcessed">
<attributes>
<id name="id">
<generated-value strategy="sequence" generator="sequenceGenerator"/>
</id>
</attributes>
</entity>
</entity-mappings>