-->
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.  [ 11 posts ] 
Author Message
 Post subject: Changing Primary key and with save but get error
PostPosted: Fri Feb 16, 2007 2:33 am 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
I save an objecting using a temp. primary key. I then change the key to another number. When I try to save the object, I get



org.hibernate.HibernateException: Don't change the reference to a collection with cascade="all-delete-orphan": uchicago.nsit.accts.data.valueobject.DPHeaderVO.account_list

session2 = sf.openSession();
Transaction trans2 = session2.beginTransaction();
session2.clear();
session2.flush();
transfer.setControlNumber(ctNumber.getControlNumber());
session2.save(transfer);
session2.flush();
trans2.commit();
session2.close();


transfer was loaded previously and the control number changed.

<hibernate-mapping>
<class name="uchicago.nsit.accts.data.valueobject.DPHeaderVO" table="DX_HEADER" schema="DBAACCTS" lazy="false" >
<id name="controlNumber" >
<column name="CONTROLNUM" />
<generator class="assigned" />
</id>

<property name="offsetApprover" >
<column name="OFFSETAPPROVER" length="32" />
</property>
<set
lazy="false"
name="account_list"
cascade="all,delete-orphan"
inverse="true"
outer-join="true"
fetch="join"
order-by="linenum asc">
<key column="CONTROLNUM"/>
<one-to-many entity-name="uchicago.nsit.accts.data.valueobject.DPAccountVO" />
</set>
<set
lazy="false"
name="compilation_list"
cascade="all,delete-orphan"
inverse="true"
fetch="join">
<key column="CONTROLNUM"/>
<one-to-many entity-name="uchicago.nsit.accts.data.valueobject.DPCompilationVO"/>
</set>
<set
lazy="false"
name="line_items"
cascade="all,delete-orphan"
inverse="true"
outer-join="true"
fetch="join"
order-by="linenum asc">
<key column="CONTROLNUM"/>
<one-to-many entity-name="uchicago.nsit.accts.data.valueobject.DPLineItemVO"/>
</set>
<set name="review_list"
lazy="false"
inverse="true"
cascade="all,delete-orphan"
outer-join="true"
fetch="join"
order-by="review_date asc">
<key column="CONTROLNUM"/>
<one-to-many entity-name="uchicago.nsit.accts.data.valueobject.DPApproverReviewVO" />
</set>
</class>

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 10:32 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Changing the primary key of an object breaks the principle of it, because you're just saving a new object b/c it has a new ID.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 11:41 am 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
I am trying to just create new rows with a new ID. Should I do a deep copy of the object and then save it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 5:10 pm 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
I have a table that uses a temporary primary key. The temporary key is only used on a row until the row is active in the program. Once it is active, the primary key changes to a new key.

I can add a new row and delete the old one but the problem is that I can't used cascade="delete-orphan" . If I use this option I cannot update my row correctly. It is like a catch22. If I use it, I break the save, if I don't use it, I break the update....

long tmpControlNo = transfer.getControlNumber();
// get new permanant PK
DPControlNoVO ctNumber = new DPControlNoVO();
ctNumber.setCreateDate(new Timestamp((new java.util.Date()).getTime()));
ctNumber.setPreparer(username);
Long ctVOId = (Long)session.save(ctNumber);
session.flush();
trans.commit();
session.close();


session1 = sf.openSession();
Transaction trans1 = session1.beginTransaction();
ctNumber = (DPControlNoVO)session1.load(DPControlNoVO.class, ctVOId);
session1.flush();
trans1.commit();
session1.close();


// Save the row with new PK
session2 = sf.openSession();
Transaction trans2 = session2.beginTransaction();
transfer.setControlNumber(ctNumber.getControlNumber());
session2.save(transfer);
session2.flush();
trans2.commit();
session2.close();

// Delete temporary info.
session = sf.openSession();
trans = session.beginTransaction();
DPHeaderVO tmpHeader = (DPHeaderVO)session.load(DPHeaderVO.class, tmpControlNo);
session.delete(tmpHeader);
session.flush();
trans.commit();
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 5:19 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
why are you saving it to the db then updating the primary key? can't you get the correct primary key before the original save

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 5:27 pm 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
There is a requirement to save transactions using a temporary number - go figure and then assign a "real" number to conserve values...... When the row is using a temporary number, it is able to be altered by the initiator....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 5:44 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Lol, that requirement blows.

Anyways, you'll need to load the whole thing into memory, detach it from the session, change the primary key, and make sure it's not stored anywhere else, then save it again in a new session. Hibernate will just treat it like a new instance. Also make sure you clear out any fields that get values auto-generated so it still looks like a new object to hibernate.

But be sure to go back and delete the old one, unless you want to fill up your database with duplicates.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 5:48 pm 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
Yeah, I have been trying to do that, it is a pain..... I was hoping for an easier solution.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 6:47 pm 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
I still get this


Don't change the reference to a collection with cascade="all-delete-orphan": uchicago.nsit.accts.data.valueobject.DPHeaderVO.account_list

session2 = sf.openSession();
Transaction trans2 = session2.beginTransaction();

// DPHeaderVO cpTransfer = (DPHeaderVO)ObjectCloner.deepCopy(transfer);
transfer.setControlNumber(ctNumber.getControlNumber());
transfer.getReview_list().clear();
transfer.getCompilation_list().clear();
Iterator newIt =transfer.getAccount_list().iterator();
while (newIt.hasNext()) {
DPAccountVO tmpAcct=(DPAccountVO) newIt.next();
tmpAcct.setHeader(transfer);
tmpAcct.setDistributionid(0);

}
newIt =transfer.getLine_items().iterator();
while (newIt.hasNext()) {
DPLineItemVO tmpLineIt=(DPLineItemVO) newIt.next();
tmpLineIt.setHeader(transfer);
tmpLineIt.setLineid(0);

}

session2.save(transfer);
session2.flush();
trans2.commit();
session2.close();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 1:43 pm 
Newbie

Joined: Wed Jan 10, 2007 1:40 pm
Posts: 12
The only way I could get this to work was to recreate by collections and main object and save it. using any objects with a save will not work. I need to use delete-orphan option which caused me an issue as well with the save.

If someone has a better way of doing this, please let me know. Clearing out all the keys and doing a save does not seem to work for me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 5:44 pm 
Newbie

Joined: Sun Feb 25, 2007 12:46 pm
Posts: 4
Did you find any solution to this? I have stuck to a similar problem.


Thanx
-00-
Dim


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