-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Multiple class one-to-many mapping problem.
PostPosted: Thu Aug 31, 2006 1:17 pm 
Newbie

Joined: Thu Aug 31, 2006 3:56 am
Posts: 2
So my problem is as follows. I have several classes (ParentOne, ParentTwo, ...) which all have a set of children associated with them. This is a one to many mapping, which would be simple with one class. However, I'd like all parents to map their sets of children to the same table. Each parent has a guid associated with it so colliding keys are not an issue.

Mapping two different classes (each with their own table) to the same common "child" table is proving troublesome to me. Hibernate throws a JDBC error (presumably because there's an FK violation). In my actual (oracle) schema, I don't have any foreign keys between these tables, so I would presume that it won't be an issue (after looking at the sql that hibernate generates). However, this is breaking my tests because of the schema that hibernate is generating for me. I'd like to avoid turning this feature off. If anyone has a good solution (something like top-level collections?) to the problem, I'd appreciate your help very much.

Note, this output has been simplified and sanitized as this is more a conceptual problem than a technical problem. Thanks!

Hibernate version: 3.1 rc2

Mapping documents:
Code:
...
<class name="ParentOne" table="PARENTS_ONE">
     <id name="guid" column="PARENT_ONE_ID"/>
     <property name="name" column="NAME" not-null="true"/>
     <set name="childMap"
             table="CHILDREN_MAP"
             lazy="false"
             cascade="save-update,persist">
             <key column="ENTITY_ID"/>
             <one-to-many class="Child"/>
     </set>
</class>

<class name="ParentTwo" table="PARENTS_ONE">
     <id name="guid" column="PARENT_ONE_ID"/>
     <property name="name" column="NAME" not-null="true"/>
     <set name="childMap"
             table="CHILDREN_MAP"
             lazy="false"
             cascade="save-update,persist">
             <key column="ENTITY_ID"/>
             <one-to-many class="Child"/>
     </set>
</class>

<class name="Child" table="CHILDREN_MAP">
      <id column="CHILD_ID" type="long">
           <generator_class="sequence"/>
      </id>
      <property name="entityId" column="ENTITY_ID" type="string" not-null="true"/>
      <property name="name" column="NAME" type="string"/>
</class>

...


Code between sessionFactory.openSession() and session.close():

Code:
ParentOne parent1 = new ParentOne("Bob");
ParentTwo parent2 = new ParentTwo("Alice");

session.save(ParentOne);
session.save(ParentTwo);

try{session.getTransaction().commit();} catch (GenericJDBCException ex) {
   throw new Exception(ex);
}


Full stack trace of any exception that occurs:
09:32:45,443 DEBUG AbstractBatcher:317 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
09:32:45,443 DEBUG AbstractBatcher:459 - closing statement
09:32:45,446 DEBUG JDBCExceptionReporter:63 - Could not execute JDBC batch update [insert into CHILDREN_MAP (NAME, ENTITY_ID, CHILD_ID) values (?, ?, ?)]
java.sql.BatchUpdateException: failed batch
at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:345)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
...


Name and version of the database you are using:
using HSQL for testing.

The generated SQL (show_sql=true):
create table CHILDREN_MAP (CHILD_ID bigint not null, ENTITY_ID varchar(255) not null, NAME varchar(255), primary key (CHILD_ID))

alter table CHILDREN_MAP add constraint FK59C29976E46D759 foreign key (ENTITY_ID) references PARENTS_ONE

alter table CHILDREN_MAP add constraint FK59C299764881A2FA foreign key (ENTITY_ID) references PARENTS_TWO

insert into CHILDREN_MAP (ENTITY_ID, NAME, CHILD_ID) values (?, ?, ?)

Debug level Hibernate log excerpt:
DEBUG

Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.