-->
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: How to update a record using session.save
PostPosted: Tue Dec 12, 2006 2:46 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
the following code creates a record with id 6 what if i just want to update id 6 to a new first name what will i have to do...

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 3:27 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Code:
Contact myContact = session.load(Contact.class, new Integer(6));
myContact.setFirstName("new name");
session.update(myContact);

_________________
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: Wed Dec 13, 2006 7:36 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
in this method does "session.load(Contact.class, new Integer(6)); " does integer(6) represent the primary key.


Top
 Profile  
 
 Post subject: org.hibernate.HibernateException: More than one row with the
PostPosted: Wed Mar 14, 2007 3:21 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
I am getting the following error when I am trying to update some of my records.

org.hibernate.HibernateException: More than one row with the given identifier was found: 3333, for class: com.dao.hibernate.TaskItem
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:135)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:81)
org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:161)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:889)
org.hibernate.impl.SessionImpl.load(SessionImpl.java:808)
org.hibernate.impl.SessionImpl.load(SessionImpl.java:801)
com.accuserverx.accucharge.dao.hibernate.UpdateWorkListDAO.update(Unknown Source)
com.accuserverx.accucharge.web.UpdateWorkListAction.execute(Unknown Source)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

I am doing the update like this


Contact myContact = session.load(Contact.class, new Integer(6));
myContact.setFirstName("new name");
session.update(myContact);
session.flush();
session.clear();


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.