-->
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.  [ 4 posts ] 
Author Message
 Post subject: problem with list when rearranging
PostPosted: Wed Nov 05, 2008 3:46 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Class A has a child list specifying an association to another class B.

Table A Table B
Id Id

Table C(Association Table with 3 columns)
AId(Id of Table A)
BId(Id of Table B)
SeqNo


When I was trying to rearrange the child list, obviously an index will be changing.

For example, the list I have orignally was

Original java and persisted list :
A1 B1 0
A1 B2 1
A1 B3 2

After rearranging the java list contains :
A1 B2 0
A1 B1 1
A2 B3 2

But when this list is getting persisted, hibernate is trying to do an update by seq no

update tablec set table Bid = B2 where Aid = A1 and SeqNo = 0

it is failing because we had set an unique index on BId which does not let hibernate to persist the same Bid if it is already persisted.

That means hibernate was trying to persist like this, that caused the failures.
A1 B2 0
A1 B2 1
A1 B3 2

Here is my hibernate mapping in classA mapping file.
<list name="assocList"
lazy="true"
access="field"
cascade="save-update"
outer-join="false"
table="tablec">
<key column="aid" not-null="true" unique="false"/>
<list-index column="seqno"/>
<composite-element class="ClassC">
<many-to-one name="ClassB" column="bid" lazy="proxy"/>
</composite-element>
</list>

Any ideas on how to resolve the issue?

Thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 4:10 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In this case it is not possible to do the change with only UPDATE sql statements, since you will always get a duplicate for the first update. Instead you have to force Hibernate to first DELETE all links and then INSERT the new elements. This can be done by de-referencing the original list. Eg. The A.getAssocList() should return a different list object than Hibernate passed to the A.setAssocList(). See http://www.hibernate.org/hib_docs/v3/re ... shotdelete

The de-referencing should only be done when you actually need it, otherwise Hibernate will delete an re-insert the elements in the list every time A is used in a transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 4:14 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
thanks for the reply. appreciate it.

There is no way that hibernate could update the seqno(index) for the given aid and bid?

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:32 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
That would also generate a similar problem where Aid and SeqNo are duplicated and I guess those two columns make up the primary key (at least if you have used Hibernate's schema generation tool to create the tables).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.