-->
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: Collection tries to re-insert existing data on update
PostPosted: Mon Sep 22, 2003 8:57 pm 
Newbie

Joined: Thu Aug 28, 2003 2:22 pm
Posts: 5
I have a User object with a property of type bag which is a collection of objects representing IDs of items the user wishes to view. When the user attempts to update this collection, I get a primary key constraint violation, which I assume is due to Hibernate trying to re-insert pre-existing data. Here is the code for my update:

User u = uw.getUser();
u.getDesiredItems().clear(); // clear out old values
String[] items = (String[])PropertyUtils.getProperty(form, "itemId");
ArrayList itemList = new ArrayList();

for (int i = 0; items != null && i < items.length; i++) {
DesiredItem di = new DesiredItem();
di.setItemType("DESIRED_ITEM");
di.setItemValue(items[i]);
di.setUser(u);
itemLst.add(di);
}

u.setDesiredItems(itemList);

SessionFactory sf = (SessionFactory)JNDIUtils.lookup("java:/hibernate/HibernateFactory");
Session s = null;
Transaction transaction = null;
if (sf == null) {
System.out.println("Could not get a handle on the SessionFactory!!");
// throw an error
} else {
try {
s = sf.openSession();
transaction = s.beginTransaction();
s.saveOrUpdate(u);
transaction.commit();
} catch (HibernateException he) {
System.out.println("Could not update user: HibernateException: " + he.getMessage());
} finally {
try {
if (s != null) {
s.close();
}
} catch (HibernateException he2) {
System.out.println("Could not close session: HibernateException: " + he2.getMessage());
}
}
}

Hibernate should be deleting the collection first, then re-inserting the new data, right? I have default-cascade set to save-update for the User mapping. Any thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 11:04 am 
Regular
Regular

Joined: Tue Sep 09, 2003 9:37 pm
Posts: 56
Location: Ogden, Utah, USA
It would be helpful if you could post your hibernate mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 2:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
make sure your unsaved-value setting is correct, and read "Parent / Child Relationship".


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.