当前位置: 动力学知识库 > 问答 > 编程问答 >

jpa - Hibernate universal foreign key with one-to-many relationship

问题描述:

I'm using unidirectional one to many relationships in my model. I have a few tables that I would like to be children to many other tables. I was hoping to accomplish this with a universal foreign key. I'm using UUIDs as surrogate keys so collisions aren't an issue.

Hibernate doesn't like it though. Is this possible using Hibernate or am I forced to define a foreign key per relationship in my shared child tables?

Forgive the Scala. It should still illustrate the point.

Classes and mappings:

@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 = _

...

}

When the Persistent Unit fires up I get an exception like:

 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)

网友答案:

The Exception is pretty clear IMHO: in your Coordinate class you have multiple fields, that is mapped to the same column generic_fk (I can't say which is the others, as you have shown us only a part of the class). As from your code, I understand there must be at least 3, because the genericFk field is already marked as insertable=false, updatable=false. Also be sure that your entity classes are deployed only once.

分享给朋友:
您可能感兴趣的文章:
随机阅读: