实体使用表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*/
initialValue = 10, /*0 by default*/
allocationSize = 100 /*50 by default*/)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "id_gen")
private long id;
所有实体使用相同的策略和相同的生成器,因此我将表生成器defenition移动到jpa-mapping xml文件中。 maven项目中的位置:
main/resources/db/jpa/orm.xml
现在,生成器在jpa-mapping 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" />
现在实体有以下id字段:
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "id_shared_generator")
private long id;
我根本不使用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时出错 path resource [spring / repository-config.xml]:init的调用 方法失败;嵌套异常是 javax.persistence.PersistenceException:[PersistenceUnit:default] 无法构建EntityManagerFactory ...由以下原因引起: org.hibernate.AnnotationException:未知Id.generator: id_shared_generator
请告诉我,我做错了什么?
是否允许通过emfb.setMappingResources("db/jpa/orm.xml");
调用设置其他jpa映射xml文件?