首页新闻找找看学习计划

求教JPA Hibernate中如何让多个不同实体共用同一个@TableGenerator

0
[待解决问题]

定义了@TableGenerator的实体

@Entity
@TableGenerator(name = "my_id_generator_table",
table = "MY_SEQ_TABLE",
pkColumnName = "pkCol",
pkColumnValue = "pkVal",
valueColumnName = "NEXT_V")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "my_id_generator_table")
private Long id;

private String name;

 // ...
}

另一个想共用@TableGenerator的实体
@Entity
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "my_id_generator_table")
private Long id;

private String name;

// ...
}

测试代码:
@Test
public void testBasicUsage() {
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
for (int i = 0; i < 12; i++) {
em.persist(new Course("course " + (i + 1)));
em.persist(new Teacher("teacher " + (i + 1), "title " + (i + 1)));
}
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
}
}

报错:

org.hibernate.AnnotationException: Unknown Id.generator: my_id_generator_table

at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:664)
at org.hibernate.cfg.AnnotationBinder.processId(AnnotationBinder.java:2365)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2270)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:913)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:740)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.gregg.hibernate.identifiers.generated_identifier_values.table.TableGenerationTest.createEMF(TableGenerationTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)

 

想问一下:是代码哪里写错了吗?还是@TableGenerator不能让多个不同实体共用?

glcn的主页 glcn | 菜鸟二级 | 园豆:202
提问于:2016-10-21 01:54
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册