我在模型中使用单向一对多关系。我有几张桌子,我想成为许多其他桌子的孩子。我希望通过通用外键实现这一目标。我使用UUID作为代理键,因此冲突不是问题。
Hibernate不喜欢它。这可能是使用Hibernate还是我被迫在我的共享子表中为每个关系定义一个外键?
原谅Scala。它仍应说明这一点。
类和映射:
@javax.persistence.Table(name = "ac_section")
@javax.persistence.Entity
class Section {
@javax.persistence.Id
@GeneratedValue(generator = "uuid")
@org.hibernate.annotations.GenericGenerator(name = "uuid", strategy = "uuid2")
@org.hibernate.annotations.Type(`type`="pg-uuid")
@BeanProperty var id : UUID = UUID.randomUUID()
...
@scala.beans.BeanProperty
@javax.persistence.OneToMany(fetch=javax.persistence.FetchType.EAGER, cascade=Array(javax.persistence.CascadeType.ALL), orphanRemoval = true)
@javax.persistence.JoinColumn(name="generic_fk", referencedColumnName = "id", nullable = false, updatable = false, insertable = false)
var sectionCoords:java.util.List[Coordinate] = new java.util.LinkedList[Coordinate]
}
@javax.persistence.Table(name = "ac_spot")
@javax.persistence.Entity
class Spot {
@javax.persistence.Id
@GeneratedValue(generator = "uuid")
@org.hibernate.annotations.GenericGenerator(name = "uuid", strategy = "uuid2")
@org.hibernate.annotations.Type(`type`="pg-uuid")
@BeanProperty var id : UUID = UUID.randomUUID()
@org.hibernate.annotations.Type(`type`="pg-uuid")
@scala.beans.BeanProperty
@javax.persistence.Column(name = "section_id", insertable = false, updatable = false)
var sectionId:java.util.UUID = _
...
@scala.beans.BeanProperty
@javax.persistence.OneToMany(fetch=javax.persistence.FetchType.EAGER, cascade=Array(javax.persistence.CascadeType.ALL), orphanRemoval = true)
@javax.persistence.JoinColumn(name="generic_fk", referencedColumnName = "id", nullable = false, updatable = false, insertable = false)
var spotCoords:java.util.List[Coordinate] = new java.util.LinkedList[Coordinate]
}
@javax.persistence.Table(name = "ac_coordinate")
@javax.persistence.Entity
class Coordinate {
@GeneratedValue(generator = "uuid")
@org.hibernate.annotations.GenericGenerator(name = "uuid", strategy = "uuid2")
@org.hibernate.annotations.Type(`type`="pg-uuid")
@BeanProperty var id : UUID = UUID.randomUUID()
@org.hibernate.annotations.Type(`type`="pg-uuid")
@scala.beans.BeanProperty
@javax.persistence.Column(name = "generic_fk", insertable = false, updatable = false)
var genericFk:java.util.UUID = _
...
}
当持久单元启动时,我得到一个例外:
javax.persistence.PersistenceException: [PersistenceUnit: testPU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.aziosoft.auctoweb.util.TestBase.before(TestBase.scala:22)
...
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.aziosoft.auctoweb.model.Coordinate column: generic_fk (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:696)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:718)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:740)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:493)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1786)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
答案 0 :(得分:0)
异常非常清楚恕我直言:在您的Coordinate
班级中,您有多个字段,这些字段映射到同一列generic_fk
(我不能说哪个是其他字段,如您所示我们只是课程的一部分)。从您的代码中我知道必须至少有3个,因为genericFk
字段已标记为insertable=false, updatable=false
。还要确保您的实体类只部署一次。