-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many entity class saved to db but seen as transient
PostPosted: Wed Feb 20, 2008 4:00 pm 
Newbie

Joined: Tue Feb 19, 2008 10:25 pm
Posts: 5
I am in the process of migrating to hibernate3, but I'm currently experiencing a problem in hibernate2. If there is another location I should be going, please let me know.

I'm working with two model objects with independent lifecycles: Agreement and Committee respectively. I am creating a CommitteeAssignment many-to-many entity object to save the following:

Code:
committee_assignments
-----------------------------
agreement_id
committee_id
term_start
term_end
...


Saving one CommitteeAssignment goes all the way to the db, no problems. When I add another and try to save, the transaction attempts to insert both, causing a duplicate key violation.

My setup seems to cause Hibernate not to see the first saved CommitteeAssignment as persistent. I verified by saving the first, and them re-attaching the Agreement object graph with a session.lock() and i got a "cannot lock tranisient instance: CommitteeAssignment" (please NOTE that the object was already saved through to the database.

Here are the players:

in Agreement.hbm.xml....

Code:
<set name="committeeAssignments" lazy="true" inverse="true"
       cascade="all-delete-orphan">
    <key column="agreement_id"/>
    <one-to-many class="........CommitteeAssignment"/>
</set>


in CommitteeAssignment

Code:
<composite-id name="id" class="........CommitteeAssignemt$Id"
        unsaved-value="any">
    <key-property name="agreementId" column="agreement_id"/>
    <key-property name="committeeId" column="committee_id"/>
</composite-id>
<many-to-one  name="agreement class="........Agreement"
        column="agreement_id" insert="false" update="false"/>
<many-to-one  name="agreement class="........Committee"
        column="committee_id" insert="false" update="false"/>


In Committee is the same as in agreement, but with key column of "committee_id"

Thanks in advance.


Last edited by MarkHolmes on Fri Feb 22, 2008 1:27 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: many-to-many entity class saved to db but seen as transi
PostPosted: Wed Feb 20, 2008 7:19 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
How do you add the second one? Meanwhile you might want to look into <key-many-to-one ...>, and please put your code parts in a code tag so that it shows up nicely. It's hard to read codes with regular fonts.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 1:40 pm 
Newbie

Joined: Tue Feb 19, 2008 10:25 pm
Posts: 5
Thanks for looking in.

I am adding new CommitteeAssignment objects to both sets (in Agreement and Committee (NOTE: Committee objects already exist in the db and are looked up for each add).

The first process is:

1. new session
2. begin
3. add new CommitteeAssignment to both collections
4. saveOrUpdate(Agreement)
5. commit
6. close session

This saves the first record to committee_assignments in the database.

The second process has the same steps as the first, except with a new CommitteAssignment (same Agreement, different Committee).

This is what triggers the unique key violation on the committee_assignments table. I notice each I add another assignment to make the collection size (n), there are (n) inserts in the sql log file.

Separately, after adding the first assignment to the collection and saved to the db, i've tried locking the Agreement object to re-associate to a new session, and that triggers a "can't lock unsaved transient value: CommitteeAssignment)" exception.

Hibernate can't tell that any of my CommitteeAssignment objects are persisted.

Also to NOTE: I can successfully add any number of assignments in a junit test case, each in different transactions and sessions. Moving into the web tier is where i find the problems. However, I use the same persistence strategy and API in both the test and the web, save for the fact that the web is facilitated by a request listener.

I just can't figure this one out.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 1:54 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
MarkHolmes wrote:
This is what triggers the unique key violation on the committee_assignments table. I notice each I add another assignment to make the collection size (n), there are (n) inserts in the sql log file.



Ah that explains everything. unsaved-value="any" makes saveOrUpdate act like save so there will be no update. You can simply remove the setting if you are upgrading to the version 3.



Farzad-


Top
 Profile  
 
 Post subject: Went with collection of components
PostPosted: Fri Mar 07, 2008 8:33 pm 
Newbie

Joined: Tue Feb 19, 2008 10:25 pm
Posts: 5
I went with a collection of components since the upgrade to Hibernate3 is a bit off for us.

Thanks Farzad for your feedback!


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