-->
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.  [ 3 posts ] 
Author Message
 Post subject: One to Many Association Using Join Tables Bug/Error ???
PostPosted: Wed Jun 20, 2007 9:00 pm 
Newbie

Joined: Tue Oct 24, 2006 11:43 pm
Posts: 5
Consider this unidirectional one-to-many relation done using join tables.
Person --> Address (*).

The hibernate mapping is pretty much the same as
http://www.hibernate.org/hib_docs/v3/re ... l-join-12m
Though I am using <list> rather than a <set>

1st, the DDL generated by the hbm2ddl tool, is not like what that site above gives, so I really dont know what to consider as a good example to use for DDL. Not sure why hibernate docs dont reflect what the tool might generate. Anyways ignoring that following is the DDL created by the hbm2ddl tool.

Table PersonAddress

Person_id varcahr not null
Address_id varchar not null unique
Person_list_index integer not null
primary key (Person_id, Person_list_index)


its not logically much different from whats given in the above link, instead having 'unique' on Address_id, there is a primary key. Anyways...


I create a person1 - person_id=1.
I create add1, add2, add3 - address_id=2,3,4 respectively.
then I add all 3 addresses to the list in person1.
persist it to database.
all good.

I get person1 back.
I remove add1.
save person1 back.
CONSTRIANT VIOLATION HERE DUE TO DUPLICATE KEY.

looking at what hibernate does seems wierd...

So after 1st save and before remove this is whats in the database -

person_id---person_list_index---address_id
1--0--2
1--1--3
1--2--4

basically shows the one to many there.

I want to remove the record no. 1--0--2. That is the 1st one.
So with the 1st row gone, hibernate needs to shift other records up so that the new list index is assingned.

expected output----

1--0--3
1--1--4

This is how hibernate proceeds
Step1: save info and delete on 1--2--4, to get rid of that last record.
Step2: update 1--0--2 to 1--0--3

BANG...CONSTRAINT VIOLATION DUE TO DUPLICATE KEY. Why? because "3" in that column already there and that column is "unique" according to DDL above or primary key according to DDL in that link above.

Step3: update 1--0--3 to infor saved from step1, that is to 1--1--4.


But cant persist this since step 2 fails.

Am I doing something wrong?
Is hibernate doing something wrong?
Am I assuming the worng thing hibernate is trying to do?

From what I saw, its shifting value from rows above and in the 1st copy above it violates the constrint.

help?
Thanks much.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 12:52 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi


You can use cascade attribute to manipulate your transaction.


for more information

http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html

at sub topic 10.11. Transitive persistence


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 1:20 am 
Newbie

Joined: Tue Oct 24, 2006 11:43 pm
Posts: 5
amila733 wrote:
Hi


You can use cascade attribute to manipulate your transaction.


for more information

http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html

at sub topic 10.11. Transitive persistence


Amila

(Don't forget to rate if helps)


Doesnt that be more useful when there is a parent child relationship? In the above case I want it as a simple association.

So removing an Address from the list in Person, shouldnt delete that address, but should just "un-associate it"

I am using cascades for compositions though
A <>---- B


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