-->
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.  [ 2 posts ] 
Author Message
 Post subject: List-to-list / implicit polymorphism / both sides abstract
PostPosted: Mon Nov 14, 2005 3:59 pm 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
Using rc2. Has anyone successfully implemented a Hibernate mapping of a many-to-many association, where we're using the implicit-polymorphism approach (mapping leafs classes only) and both sides of the association are abstract attributes (non-leaf definitions)? Also, both sides of the association are lists.

Here is the essence of my mapping effort:

<hibernate-mapping>
<class name="test.nm.ObjectA" table="DS_ObjectA">
<id name="dsId" type="long" column="DSID"/>
<list name="bList" table="DS_aList_bList" cascade="all" inverse="true">
<key column="DS_ObjectA_PK" not-null="true"/>
<list-index column="SORT_ORDER2"/>
<many-to-any meta-type="org.hibernate.type.StringType" id-type="long">
<meta-value value="DS_ObjectB" class="test.nm.ObjectB"/>
<column not-null="true" name="DS_ObjectB_TABLE"/>
<column not-null="true" name="DS_ObjectB_PK"/>
</many-to-any>
</list>
</class>
<class name="test.nm.ObjectB" table="DS_ObjectB">
<id name="dsId" type="long" column="DSID"/>
<list name="aList" table="DS_aList_bList" cascade="all" inverse="false">
<key column="DS_ObjectB_PK" not-null="true"/>
<list-index column="SORT_ORDER1"/>
<many-to-any meta-type="org.hibernate.type.StringType" id-type="long">
<meta-value value="DS_ObjectA" class="test.nm.ObjectA"/>
<column not-null="true" name="DS_ObjectA_TABLE"/>
<column not-null="true" name="ObjectA_PK"/>
</many-to-any>
</list>
</class>
</hibernate-mapping>

This compiles ok and the join table is created.

create table DS_aList_bList (DS_ObjectA_PK not null, DS_ObjectB_TABLE not null, DS_ObjectB_PK not null, SORT_ORDER2 not null, DS_ObjectA_TABLE not null, SORT_ORDER1 not null, primary key (DS_ObjectB_PK, SORT_ORDER1))

This misses a uniqueness constraint on DS_ObjectA_PK + SORT_ORDER2, but no matter.

I can persist ObjectA and Object B. Yet when I persist an association between the two, hibernate fails. Dealing with the association from the ObjectB "side", it deletes all records from DS_aList_bList matching ObjectB as it should, then it attempts an insert. The insert statement is:

insert DS_aList_bList (DS_ObjectB_PK, SORT_ORDER1, DS_ObjectA_TABLE, ObjectA_PK) values (1, 0, 'DS_ObjectA', 2)

This insert statement fails because of the not-null constraint on DS_ObjectB_TABLE and SORT_ORDER2.

And that's the problem.

Note that one cannot remove the not-null constraints on a list-index column, so I cannot make SORT_ORDER2 nullable, even if I wanted to.

--------------
Aside: Note the meta-type="org.hibernate.type.StringType". Its not well documented (AFAIK), but if you omit this, Hibernate will ignore all your meta-value stuff and write the classname into the column rather than using the value attribute in the the meta-value element.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 1:26 am 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
Forget the distraction of "implicit polymorphism" and "both sides abstract". They're irrelevant.
Be warned that Hibernate cannot support list-to-list at all (A many-to-many association, when both sides are ordered).

Look at the following mapping:

Code:
<class name="Person">
   <id name="id" column="personId">
      <generator class="native"/>
   </id>
   <list name="addresses" table="PersonsAddresses">
      <key column="personId" not-null="true"/>
      <list-index column="personSortOrder"/>
      <many-to-many class="Address" column="addressId"/>
   </list>
</class>
<class name="Address">
   <id name="id" column="addressId">
      <generator class="native"/>
   </id>
   <list name="people" table="PersonsAddresses">
      <key column="addressId" not-null="true"/>
      <list-index column="addressSortOrder"/>
      <many-to-many class="Person" column="personId"/>
   </list>
</class>

This will fail in Hibernate. The problem is that Hibernate updates the association from only one side, and that side is ignorant of the sortOrder column of the other side. So, e.g., it will update Person and attempt to insert a new row into the PersonAddresses table with column addressSortOrder as null, obviously causing an exception. no amount of 'inverse="true" trickery helps.

If you're willing hold your nose, you can always generalize the "make the index a 'property'" workaround and write a "myIndexInTheOtherGuysList" method.


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

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.