-->
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.  [ 7 posts ] 
Author Message
 Post subject: Save me, for Using find and save.
PostPosted: Sat May 21, 2005 5:57 am 
Newbie

Joined: Fri Jul 23, 2004 8:35 am
Posts: 6
Session s = HibernateSession.currentSession(); // open a new session
Criteria c =s.createCriteria(User.class);
c.add(Restrictions.eq("id",id.trim())); // set uuid.hex for id
List<User> l = c.list();
User u = l.get(0);
u.setId("");
u.setUsername("public china");
s.saveOrUpdate(u); // for adding a new record
s.beginTransaction().commit();

show error as following:

Hibernate: select this_.id as id0_, this_.username as username1_0_, this_.password as password1_0_, this_.nickName as nickName1_0_, this_.website as website1_0_, this_.type as type1_0_, this_.address as address1_0_, this_.home_telephone as home8_1_0_, this_.mobile_telephone as mobile9_1_0_, this_.realName as realName1_0_ from user this_ where this_.id=?
org.hibernate.HibernateException: identifier of an instance of com.nbnh.enterprise.po.User altered from 4028809303f909db0103f909e0760001 to
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 org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.nbnh.enterprise.dao.UserDAOImpl.findUserById(UserDAOImpl.java:40)
at test.dao.TestDAO.userDAO(TestDAO.java:22)
at test.dao.TestDAO.testAllDAO(TestDAO.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
[/list]

_________________
My email address bounced and was removed by an administrator. I have to update my profile.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 3:23 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I don't understand why you are trying to change an object's ID.
If you want a new object, create a new one and have hibernate save it and set its ID.

Hibernate is warning you that you are changing the id of a non transient object. This makes no sense and Hibernates does not allow you to do that.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 7:40 am 
Newbie

Joined: Fri Jul 23, 2004 8:35 am
Posts: 6
my mean is Change one of fields value.
first, i found a object with id, and change it status.

User u = dao.findUserById("329a9d9329329329a");
u.setStatus("locked");
dao.saveOrUpdate(u);// save statusfor user;

above is error. i don't know why run error.
when i add session.close() after dao.findUserByid() method, i run well.

_________________
My email address bounced and was removed by an administrator. I have to update my profile.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 7:53 am 
Newbie

Joined: Fri Jul 23, 2004 8:35 am
Posts: 6
[quote="mrfeng"]my mean is Change one of fields value.
found a user,showing on page.Update one of the fields.
[quote]

_________________
My email address bounced and was removed by an administrator. I have to update my profile.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 10:52 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Well,

You can change objects attribute, but do not touch the ID and it will work fine.

As per your example: Instead of doing this:

Code:
Session s = HibernateSession.currentSession(); // open a new session
Criteria c =s.createCriteria(User.class);
c.add(Restrictions.eq("id",id.trim())); // set uuid.hex for id
List<User> l = c.list();
User u = l.get(0);
u.setId("");
u.setUsername("public china");
s.saveOrUpdate(u); // for adding a new record
s.beginTransaction().commit();

Do this:


I have removed the line where you were touching the ID, I have changed the way to execute your Criteria and the way you use your transaction. Please read carefully :)
Code:
Session s = HibernateSession.currentSession();
Criteria c =s.createCriteria(User.class);
c.add(Restrictions.eq("id",id.trim())); // set uuid.hex for id
//Since your are searching by ID, you should use Criteria.uniqueResult()
User u = (User)c.uniqueResult();
u.setUsername("public china");
Transaction trx = s.beginTransaction();
s.saveOrUpdate(u); // for adding a new record
trx.commit();


Have a nice day,
Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 9:00 pm 
Newbie

Joined: Fri Jul 23, 2004 8:35 am
Posts: 6
Thanks for vgiguere. Your answer is right.

_________________
My email address bounced and was removed by an administrator. I have to update my profile.


Top
 Profile  
 
 Post subject: Re: Save me, for Using find and save.
PostPosted: Fri Mar 31, 2006 6:05 pm 
Newbie

Joined: Tue Mar 28, 2006 12:15 pm
Posts: 2
mrfeng wrote:
org.hibernate.HibernateException: identifier of an instance of com.nbnh.enterprise.po.User altered from 4028809303f909db0103f909e0760001 to



This is a mind boggling error that I got too. And it had nothing to do with my code.

I was using save() to persist the object to the database but later on when the JSP got rendered, Webwork's tags tried to set the id of the object causing this error.

I found it by printing a stacktrace in the setId() method:


java.lang.Throwable
at com.zz.zz.vo.User.setId(User.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:491)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:785)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:937)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:76)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:132)
at com.opensymphony.xwork.util.OgnlValueStack$ObjectAccessor.setProperty(OgnlValueStack.java:63)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1629)
at ognl.ASTProperty.setValueBody(ASTProperty.java:105)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.ASTChain.setValueBody(ASTChain.java:172)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork.util.OgnlUtil.setValue(OgnlUtil.java:184)
at com.opensymphony.xwork.util.OgnlValueStack.setValue(OgnlValueStack.java:153)
at com.opensymphony.xwork.util.OgnlValueStack.setValue(OgnlValueStack.java:136)
at com.opensymphony.xwork.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:136)
at com.opensymphony.xwork.interceptor.ParametersInterceptor.before(ParametersInterceptor.java:111)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:30)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:189)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:31)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:189)
at com.opensymphony.webwork.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:171)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:189)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:31)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:189)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:31)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:189)
at com.opensymphony.xwork.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:151)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:189)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:31)


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