-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate cache batch INSERTS
PostPosted: Thu Feb 21, 2013 4:00 am 
Newbie

Joined: Wed Feb 09, 2011 11:09 am
Posts: 2
Two entities: Item, SubItem

Quote:
Item has an:
Id: int
Code: string
subitems: Collection<SubItem>

SubItem has:
Parent: Item
Item: Item
Quantity: float


Situation: Batch inserts
Quote:
item = new Item
item.Code = "1"

item2 = new Item
item2.Code = "2"

item3 = new Item
item3.Code = "3"

item4 = new Item
item4.Code = "4"

item.Add(new SubItem(item2, 50));
item.Add(new SubItem(item3, 50));

item4.Add(new SubItem(item2, 50));
item4.Add(new SubItem(item3, 50));


--> Batch insert processing
Quote:
openSession()
saveOrUpdate(item);
**** INSERT INTO ITEMS(ID, CODE) VALUES (1, "1");
****** INSERT INTO ITEMS(ID, CODE) VALUES (2, "2");
****** INSERT INTO ITEMS(ID, CODE) VALUES (3, "3");
****** INSERT INTO SUBITEMS(ID, PARENT_ID, ITEM_ID, QUANTITY) VALUES (1, 1, 2, 50);
****** INSERT INTO SUBITEMS(ID, PARENT_ID, ITEM_ID, QUANTITY) VALUES (2, 1, 3, 50);
saveOrUpdate(item4);
**** INSERT INTO ITEMS(ID, CODE) VALUES (4, "4");
****** INSERT INTO ITEMS(ID, CODE) VALUES (5, "2"); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This insert is wrong, the Item2 has inserted previously with ID=2.
****** INSERT INTO ITEMS(ID, CODE) VALUES (6, "3"); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This insert is wrong, the Item3 has inserted previously with ID=3.
****** INSERT INTO SUBITEMS(ID, PARENT_ID, ITEM_ID, QUANTITY) VALUES (3, 4, 5, 50); <<<<<<<< Should be: VALUES (1, 4, 2, 50);
****** INSERT INTO SUBITEMS(ID, PARENT_ID, ITEM_ID, QUANTITY) VALUES (4, 4, 6, 50); <<<<<<<< Should be: VALUES (1, 4, 3, 50);
closeSession()


How Can I solve that?
Second level cache?


Top
 Profile  
 
 Post subject: Re: Hibernate cache batch INSERTS
PostPosted: Thu Feb 21, 2013 7:08 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I guess you did something wrong in your mapping,
like a cascade on persist which does not recognize correctly that items 1,2 and 3 were already persisted.
Second level chache has surely nothing to do with this.
Can you report your mapping?


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