Hi,
I'm having some trouble trying to save an object to the database and I think the problem stems from one of the classes having a composite primary key which itself contains a foreign key reference to a class which it's related to through a one-to-many relationship.
Its probably easiest to explain through the code, so below are my simplified class definitions and hibernate config:
I have three classes making up a simple hierarchical structure: A
GParent has a set of
Parents which in turn has a set of
Offspring.
These map directly to three tables in the (Sybase) database: gparent, parent, and offspring.
gparent - primary key= G_PARENT_ID - an IDENTITY field.
parent - primary key: PARENT_ID - is also an IDENTITY field. The "parent" table also has a column called G_PARENT_ID, which is a foreign key reference to the G_PARENT_ID filed in the "gparent" table.
offspring - composite primary key: CHILD_ID and PARENT_ID. PARENT_ID is a foreign key reference to the PARENT_ID column of the "parent" table.
It's probably also worth mentioning that none of the columns in any of the tables allow null values, and these are legacy tables which can't be altered at all.
Code:
public class GParent {
private int gParentId;
private Set<Parent> parents
...
}
public class Parent {
private int parentId;
private Set<Offspring> offspring
...
}
public class Offspring {
private int parentId;
private int childId;
...
}
Code:
<hibernate-mapping>
<class name="GParent" table="gparent" lazy="false">
<id name="gParentId" type="int">
<column name="G_PARENT_ID" sql-type="numeric(10,0)" />
<generator class="identity" />
</id>
...
<set name="parents" inverse="false" table="parent" cascade="all">
<key column="G_PARENT_ID" not-null="true"/>
<one-to-many class="Parent" />
</set>
...
</class>
<class name="Parent" table="parent" lazy="false">
<id name="praentId" type="int">
<column name="PARENT_ID" sql-type="numeric(10,0)" not-null="true" />
<generator class="identity" />
</id>
...
<set name="offspring" inverse="false" table="offspring" cascade="all">
<key column="PARENT_ID" not-null="true"/>
<one-to-many class="Offspring"/>
</set>
</class>
<class name="Offspring" table="offspring" lazy="false">
<composite-id>
<key-property name="parentId" column="PARENT_ID" />
<key-property name="childId" column="CHILD_ID"/>
</composite-id>
...
</class>
</hibernate-mapping>
All I'm try to do is populate a gParent object (including parents and offspring) to the database. If I update my classes and config so that the Offspring doesn't exist at all (i.e. I have simply a gParent with a set of Parents), everything works fine. When I add a set of Offspring to the parent class though (and update the hibernate config as well, of course), I get following exception:
Quote:
2011-09-09 17:55:33,223 INFO [main] [OrderPersister] Hibernate Exception message: Repeated column in mapping for entity: Offspring: column: PARENT_ID (should be mapped with insert="false" update="false")
org.hibernate.MappingException: Repeated column in mapping for entity: Offspring column: PARENT_ID (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
...
Following a suggestion on another forum, I've also tried using the <key-many-to-one> tag in my Offspring class definition, as below, but I'm then presented with a different Exception which I can't seem to get round, despite trying several different approaches:
Code:
<class name="com.jpmc.am.product.oaop.maf.model.TrdAllocation" table="trd_Allocation" lazy="false">
<composite-id>
<key-many-to-one name="orderCode" column="OrderCode" />
<key-property name="subfundCode" column="SubfundCode"/>
</composite-id>
...
</class>
This config produces the following error:
Quote:
2011-09-09 18:00:51,391 INFO [main] [OrderPersister] Hibernate Exception message: An association from the table offspring refers to an unmapped class: int
org.hibernate.MappingException: An association from the table offspring refers to an unmapped class: int
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1343)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
Any help or suggestions at all would be greatly appreciated.
Cheers
Dan