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: Having problem with simple hibernate update
PostPosted: Fri Aug 22, 2008 5:24 pm 
Newbie

Joined: Wed Apr 23, 2008 3:36 pm
Posts: 6
Hi,

I am having problem doing update operation. I did save, retrieve and delete but update is giving me trouble.Can someone please tell me what am doing wrong?

I am using Hibernate version: 3.0 and the database is Oracle 10g.

My hbm file is as follows:hibernate.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Users" table="USERINFO">
<id name="ssn" column="SSN" >
<generator class="assigned"/>
</id>
<property name="name" column="NAME"/>
</class>
</hibernate-mapping>

My Code:

FirstExample.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample {
public static void main(String args[])
{
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();

//Inserting Object state into the database.
//----------------------------------------
Users user1 = new Users();
user1.setSsn(29); //Autoboxing done here.
user1.setName("Ravi");
session.save(user1);

Users user2 = new Users();
user2.setSsn(2);
user2.setName("Arul");
session.save(user2);
session.flush();
//so that results apopear immediately in the database.

//Retrieving Object state from the database.
//-----------------------------------------

Users userobj = (Users)session.load(Users.class, user1.getSsn()); // Here user1.getSsn() is acting as the serializable object.
System.out.println(userobj.getSsn());
System.out.println(userobj.getName());

Users userobj2 = (Users)session.load(Users.class, user2.getSsn()); // Here user2.getSsn() is acting as the serializable object.
System.out.println(userobj2.getSsn());
System.out.println(userobj2.getName());

//Deleting object state from the database.
session.delete(user1); //Here am deleting the above saved user1 object.
session.flush();

//Updating object state in the database.
user2.setName("Rosh");
user2.setSsn(19);
session.update(user2);
session.flush();

}catch(Exception e){
e.printStackTrace();
}


}
}

Exception stack trace:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
29
Ravi
2
Arul
org.hibernate.HibernateException: identifier of an instance of Users altered from 2 to 19
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:675)
at FirstExample.main(FirstExample.java:46)

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 24, 2008 4:47 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't think it is possible to change the primary key value of an object. Once it has been set you are stuck with it. I guess the only way is to delete the existing object and re-save it with the new ID.


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.