update doesnt work in 1 transaction!
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 !
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 !
posted 19 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
I have tried all links but couldnt succeed in performing an update.
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Show us the code you are trying, and someone will probably spot where you are going wrong.
Betsy Camel
Ranch Hand
Posts: 119
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
| This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








