-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with multiple records update...
PostPosted: Tue Apr 10, 2007 8:58 am 
Newbie

Joined: Tue Apr 10, 2007 8:22 am
Posts: 4
Hibernate version: 3

MySQL 5

I have a set of values in a hashmap which i will have to insert into the database...

The hashmap will contain values like {0.key="xxx", 1.key="yyy",...}

I try to get the values from the hashmap with a for loop and save all those values to the session. And i will flush and commit the session and traction after the loop finishes execution.

When i try to complete this task with the following code, only one insert statement is being executed.


for(int i=0; i<valueMap.size(); i++){
Keywords key = new Keywords(); //Keywords is the class for the table
key.unPack(valueMap, i); // Unpack is method which assigns values to
// the fields, Value map is the hashMap
session.clear();
session.save(key);
}
session.flush();
tr.commit();

Method unPack():
public void unPack(HashMap map, int i) {
this.keyword = map.get(i+".key").toString();
}


And if i try with this code, all the insert is being executed...


for(int i=0; i<valueMap.size(); i++){
Keywords key = new Keywords();
String id = valueMap.get(i+".key").toString();
queryStr = "from Keywords where keyword='"+id+"'";
if(queryStr!=null){
Query query = (Query)session.createQuery(queryStr);
key.unPack(valueMap, i);
if(query.list().isEmpty()){
session.clear();
session.save(key);
}
else{
session.clear();
session.update(key);
}
}
else{
objSel.unPack(valueMap, i);
session.clear();
session.save(key);
}
}
session.flush();
tr.commit();


Where might be the problem? Please help me...
Thanks is advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 9:36 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Do you know what Session.clear() does? Why are you using it all over the place? Under normal circumstances, you do not need to use this method.

http://www.hibernate.org/hib_docs/v3/ap ... tml#clear()

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 11, 2007 12:11 am 
Newbie

Joined: Tue Apr 10, 2007 8:22 am
Posts: 4
Thanks for the reply. It says session.clear() clears all saved objects...

Ok. When i dont use session.clear(), i get an exception like,
Session is already assoicated with someother object... and so on...

What should i use there instead of session.clear() ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 11, 2007 4:56 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi naren.sarma
Post your Exact Error Stack trace

_________________
Dharmendra Pandey


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