Java 在Spring容器中为jpa在xml中查找id生成器
实体使用表id生成器策略:Java 在Spring容器中为jpa在xml中查找id生成器,java,spring,jpa,configuration,Java,Spring,Jpa,Configuration,实体使用表id生成器策略: @Id @TableGenerator(name = "id_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", pkColumnValue = "shared_id_generator", /*same as generator name by default*/ initialV
@Id
@TableGenerator(name = "id_gen",
table = "id_gen",
pkColumnName = "gen_name",
valueColumnName = "gen_val",
pkColumnValue = "shared_id_generator", /*same as generator name by default*/
initialValue = 10, /*0 by default*/
allocationSize = 100 /*50 by default*/)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "id_gen")
private long id;
所有实体都使用相同的策略和相同的生成器,所以我将表生成器defentition移动到jpa映射xml文件中。maven项目中的位置:
main/resources/db/jpa/orm.xml
生成器现在在jpa映射xml文件中定义为:
<table-generator name="id_shared_generator"
table="id_gen"
pk-column-name="gen_name"
pk-column-value="shared_id_value"
value-column-name="gen_val"
initial-value="10"
allocation-size="100" />
我根本不使用persistence.xml文件,而是配置entityManagerFactory:
@Bean
@DependsOn("liquibase")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
// emfb.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
emfb.setPackagesToScan(new String[]{"com.savdev.springmvcexample.domain"});
emfb.setMappingResources("db/jpa/orm.xml");
emfb.setJpaVendorAdapter(jpaVendorAdapter());
emfb.setJpaProperties( new Properties(){{
put("hibernate.hbm2ddl.auto", "validate");
put("hibernate.format_sql", "true");
put("hibernate.show_sql", "true");
}});
emfb.setPersistenceUnitName("springmvcexample");
return emfb;
}
现在,我在尝试加载spring应用程序上下文时遇到以下异常:
原因:org.springframework.beans.factory.BeanCreationException:
创建类中定义的名为“entityManagerFactory”的bean时出错
路径资源[spring/repository config.xml]:初始化的调用
方法失败;嵌套异常是
javax.persistence.PersistenceException:[PersistenceUnit:default]
无法生成EntityManagerFactory。。。原因:
org.hibernate.AnnotationException:未知Id.generator:
id\u共享\u生成器
请告诉我,我做错了什么?
是否允许通过
emfb.setMappingResources(“db/jpa/orm.xml”)设置额外的jpa映射xml文件代码>调用?您确定映射文件位置正确吗main/resources/jpa/orm.xml
vsdb/jpa/orm.xml
?main/resources/db/jpa/orm.xml-文件夹中的位置。但是资源文件夹在类路径中,所以我只传递“db/jpa/orm.xml”字符串
@Bean
@DependsOn("liquibase")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
// emfb.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
emfb.setPackagesToScan(new String[]{"com.savdev.springmvcexample.domain"});
emfb.setMappingResources("db/jpa/orm.xml");
emfb.setJpaVendorAdapter(jpaVendorAdapter());
emfb.setJpaProperties( new Properties(){{
put("hibernate.hbm2ddl.auto", "validate");
put("hibernate.format_sql", "true");
put("hibernate.show_sql", "true");
}});
emfb.setPersistenceUnitName("springmvcexample");
return emfb;
}