Hibernate version: 3.1.2
I am having some 'fun' with what I think is a very basic use case,
which has been discussed ad nauseum before, for example here:
http://forum.hibernate.org/viewtopic.ph ... ber+childs
Actually I am reusing the example from that thread as much as possible.
My Relational model depends on composite keys for child tables,
where the first part of the primary key is also used as the foreign
key. So the tables are examplified as follows and I can't do anything about that:
Code:
CREATE TABLE Parent (
parentId INTEGER NOT NULL
);
ALTER TABLE Parent
ADD ( PRIMARY KEY (parentId) ) ;
CREATE TABLE Child (
parentId INTEGER NOT NULL,
childNumber INTEGER NOT NULL
);
ALTER TABLE Child
ADD ( PRIMARY KEY (parentId, childNumber) ) ;
Simple enough.
Since each Child belongs to a single Parent, I would like to take
advantage of a collection, 'set' or 'bag' for example.
Furthermore, I would very much prefer to have the collection
fully managed from the parent.
I have spent some time on the various variations, so please bear with
me for a second, while I lay them out for you.
For simplicity, I use entity-name so sthat I won't have to write and
provide the corresponding classes.
The simplest variant, on which I would like to solicit comments is:
Code:
<!-- ParentChild, Variant 1 -->
<class entity-name="Parent" table="Parent">
<id name="parentId" type="int">
</id>
<set name="Children" cascade="all-delete-orphan">
<key column="parentId" not-null="true" />
<one-to-many class="Child" />
</set>
</class>
<class entity-name="Child" table="Child">
<composite-id>
<key-property name="parentId" type="int"/>
<key-property name="childNumber" type="int"/>
</composite-id>
</class>
This results in the following (again this is a classic, but hang in there):
Code:
1344 [main] ERROR HibernateStore - Hibernate [odr_Hibernate] ERROR: org.hibernate.MappingException: Repeated column in mapping for entity: Child column: parentId (should be mapped with insert="false" update="false")
org.hibernate.MappingException: Repeated column in mapping for entity: Child column: parentId (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:575)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:597)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:615)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:405)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:984)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169)
at dinmar.oacis.hibernate.HibernateStore.getSessionFactory(HibernateStore.java:137)
at TestParentChild.main(TestParentChild.java:126)
On this, I have two questions:
Q1) What exactly is being checked for duplication here?
As far as I am concerned, I specified the key for the FK in Child,
which would typically be used in a where clause but not as part of
any select list.
Furthermore, why would the separately mapped class Child be
constrained to not 'duplicate' column names used elsewhere in the
mapping?
Q2) Following up on the error message, I tried various places to put
insert="false" update="false" in this mapping, but that appears to
be plain impossible (key and composite-key do not allow these
attributes)
Good advice has been handed out elsewhere, so I'll try an inverse mapping in the next episode, please stay tuned.