我在ORM中遇到继承问题。
我使用JPA / hibernate。我收到一个我不明白的错误。我已经宣布了一次专栏,但是错误告诉我,我已经完成了两次。它可能会以某种方式与继承混淆。 但我不知道为什么。请帮忙
我得到错误:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: myInsurance.models.liability.hivAndVHProtection.Hiv_AnswerForEntities column: variant_id (should be mapped with insert="false" update="false")
实体代码:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Answer extends Generic<Answer> {
@OneToOne public Variant variant;
}
@Entity
@DiscriminatorValue("Answer")
@Table(name="mi__Liability_Part_Hiv_Answer")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Hiv_Answer extends Answer {
@OneToOne public HivAndVHProtection_Variant variant;
}
@Entity
@DiscriminatorValue("AnswerForEntities")
public class Hiv_AnswerForEntities extends Hiv_Answer {
public int proffesionGroup;
public int personCount;
public int contributionPerPerson;
@Transient public int totalContribution;
}
完整堆栈跟踪:
play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.6]
at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.6]
at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:102) ~[play_2.10.jar:2.2.6]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) ~[scala-library.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1361) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library.jar:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library.jar:na]
at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.2.6]
at play.api.Play$.start(Play.scala:87) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:139) ~[play_2.10.jar:2.2.6]
... 14 common frames omitted
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: myInsurance.models.liability.hivAndVHProtection.Hiv_AnswerForEntities column: variant_id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:682) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:704) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:726) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:479) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.mapping.UnionSubclass.validate(UnionSubclass.java:61) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1283) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1734) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
... 28 common frames omitted
- 编辑 -
我在Variant
Entities
类作为关系
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Variant extends GenericDictionary<Variant> {}
@Entity
@Table(name="mi__Liability_Part_Hiv_Var")
public class HivAndVHProtection_Variant extends Variant {
@OneToMany public List<HivAndVHProtection_VariantValue> hivAndVHProtection_VariantValue = new LinkedList<HivAndVHProtection_VariantValue>();
}
@Entity
@Table(name="mi__Liability_Part_Legal_Var")
public class LegalExpensesInsurance_Variant extends GenericDictionary<LegalExpensesInsurance_Variant> {
@OneToMany private List<LegalExpensesInsurance_VariantValue> legalExpensesInsurance_VariantValues = new LinkedList<LegalExpensesInsurance_VariantValue>();
}
所有这些Variant
类都扩展了超类Variant
我也试过了:
@OneToOne public Class<? extends Variant> variant;
但得到一个错误:
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on myInsurance.models.liability.hivAndVHProtection.Hiv_Answer.variant references an unknown entity: java.lang.Class
答案 0 :(得分:0)
在Answer
你有
@OneToOne public Variant variant;
在Hiv_Answer
你有
@OneToOne public HivAndVHProtection_Variant variant;
将这两个映射的列名称默认为variant_id
,在这种情况下,JPA强制您使用insertable = "false", updateable = "false"
标记其中一个,因为您无法保持相同通过多个映射字段。
您的选择是:
variant
更改为其中一个映射中的其他内容(首选)@JoinColumn
指定不同的连接列而不是默认连接列(variant_id
)