Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update doesnt work in 1 transaction!

 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

I am trying an example in hibernate and found something interesting.
update in hibernate does the following..

1)inserts a row using save
2)gets the row using get
3)create another obj of the class
4)saves the value in the first row in the second & saves this row.


is it not possible to update a existing row? by using load a particular row and update it using update command.

when i tried this i got a exception saying

org.hibernate.HibernateException: identifier of an instance of Honey altered from 10 to 0
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at app.main(app.java:44)




can anyone give me an idea of performing the above !
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is it not possible to update a existing row?

Yes, it is possible to update an existing row.

Objects in Hibernate are identified by their ID property. If you change the ID, you are telling Hibernate you are dealing with a different object, so you will need to load/create this other object before you can update it (which is why you see an exception when you try to update it). So long as you don't change the ID you can update any loaded object to your heart's content.
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks paul for the reply, but can u give me an example for a update transaction in hibernate.
I have tried all links but couldnt succeed in performing an update.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the code you are trying, and someone will probably spot where you are going wrong.
 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;


public class app {

public static void main(String[] args) {
try{
SessionFactory sessionFactory = new Configuration().configur().buildSessionFactory();
Session session =sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Honey honey = (Honey) session.load(Honey.class, new Integer(23));
System.out.println("ID: " + honey.getFirstname());
System.out.println("First Name: " + honey.getLastname());
honey.setFirstname("HI");
session.update(honey);
session.flush();
tx.commit();
session.close();
}catch(HibernateException e){
e.printStackTrace();
}

}

this is the code i have written for updating a record.I guess the configuration & mapping files are right as i am able to perform insert, delete & select. can anyone point out the error in the above code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic