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.