-->
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.  [ 11 posts ] 
Author Message
 Post subject: Insert rolling back but persistent object updated
PostPosted: Wed Aug 31, 2005 4:21 pm 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0

I have open a session. Then I do some work with an object that I have retrieved from the database. I call saveOrUpdate(obj) and this works great. Then I call tx.commit and close the session. If I add a layer into this where I want to create an insert on an object into another table before I do this saveOrUpdate, I get the following:

org.hibernate.HibernateException: identifier of an instance of mine.db.hibernate.objects.PasswordHistory altered from 11 to null. And nothing happens in the db but I see that a rollback occurred in the logs. Higher in the stack trace I see this message:
transaction after completion callback, status:4. Not sure what status 4 might be. What might I be missing here? I want to call saveOrUpdate(pwHistoryObj) but that part is not working and rolls everything back and the other saveOrUpdate(obj) does not happen.
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 5:35 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
As the exception says, the identifier property is null, so you can't persist(update) the object. When this fails, everything else, including your other saveOrUpdate call, *IN THE SAME TRANSACTION* is rolled back.

A transaction = A UNIT of work.


Top
 Profile  
 
 Post subject: but why is it null?
PostPosted: Thu Sep 01, 2005 7:28 am 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
Thank you for your response. My question is why is it null. I am using generated id's for the Identifier field on all of my tables. This is defined in all of my .hbm files. I believe that the way saveOrUpdate() works is that if the object already exists in the db then it does not assign a new id but just updates the existing entry. If not entry is found, a new id should be assigned and then persist to the table. What is it that I am missing here?
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 8:03 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
You are right... if you try to save an existing object, hibernate will issue an update, and if you save a new object, hibernate will issue an insert...

Are you using the same object (pwHistoryObj) for creating the new one? Hibernate uses the internal java object id to decide between insert or update.
The following code would render the same error:

MyObj A;
A.setName("im a");
ses.saveOrUpdate(A);

A.setId(null);
A.setName("im a new A");
ses.saveOrUpdate(A);

In short: look for changes in the object that causes the exception, and be sure to create a new object for every new object you want to insert.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject: for Hector
PostPosted: Thu Sep 01, 2005 11:31 am 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
Here is my code:

Initialize and do some preliminary logic first then:
Analyst appUsers = new Analyst();
Session session = HibernateUtil.getSession();
AnalystDAO anDAO = new AnalystDAO();
try
{
tx.session.beginTransaction();
appUsers = anDAO.findBySids(session, userSid);
Long anID = appUsers.getAnalystID();

PasswordDAO pwDAO = new PasswordDAO();
boolean exists = false;

exists = pwDAO.usedPassword(session, anID, newPassword);
if (exists)
{
tx.commit();
return exists;
}

PasswordHistory history = new PasswordHistory();
history.setAnalystID(anID);
history.setPassword(oldPassword);
history.setPwdCreationDate(appUsers.getPwdCreationDate());
// Just in case create a new instance of dao
PasswordDAO passDAO = new PasswordDAO();
passDAO.makePersistent(session, history);

Date dte = Calendar.getInstance().getTime();
appUsers.setPassword(newPassword);
appUsers.setPwdCreationDate(dte);
anDAO.makePersistent(session, appUsers);
tx.commit();
return exists;
}

Methods in other classes referred to:

public boolean usedPassword(Session session, Long anID, String password)
{
PasswordHistory pwHistory = new PasswordHistory();
boolean found = false;
try
{
Criteria criteria = session.createCriteria(PasswordHistory.class);
pwHistory = (PasswordHistory)criteria.add(Expression.eq("password", password).add(Expression.eq("analystID", analystID)).uniqueResult();
if (pwHistory != null)
{
found = true;
}
} catch ....
return found;
}


public void makePersistent(Session session, PasswordHistory pwHistory)
{
try
{
session.saveOrUpdate(pwHistory);
} catch ....
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 11:59 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
Can you post your mapping files? The method getAnalystID() is a simple getter?

Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject: Mappings
PostPosted: Thu Sep 08, 2005 8:17 am 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
I was off for a few days so here are my mapping files, finally.

Analyst.hbm.xml

<hibernate-mapping package="hd.db.org.hibernate.objects">
<class name="Analyst" table="analyst" lazy="false">
<id name="analystID">
<generator class="native"/>
</id>
<property name="userFirstName" not-null="true"/>
<property name="userLastName" not-null="false"/>
<property name="sid" not-null="flase" unique="true"/>
<property name="password" not-null="false"/>
<property name="pwdCreationDate"/>
<many-to-one name="userRole" column="roleID"/>
</class>
</hibernate-mapping>

PasswordHistory.hbm.xml
<hibernate-mapping package="hd.db.org.hibernate.objects">
<class name="PasswordHistory" table="passwordHistory" lazy="false">
<id name="passwordHistoryID">
<generator class="native"/>
</id>
<property name="analystID" not-null=true"/>
<property name="password" not-null="true"/>
<property name="pwdCreationDate" not-null="true"/>
<class>

Thank you in advance for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 9:12 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Could you post the full stack trace and the bean code for your PasswordHistory object?

It looks like you're resetting the ID of the PasswordHistory object to null at some point, on a persisted object.


Top
 Profile  
 
 Post subject: will post
PostPosted: Thu Sep 08, 2005 10:30 am 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
I will post my stack trace and full code shortly but what I want to do is create a new passwordHistory object in the db based on the info that I retrieve out of the current analyst.


Top
 Profile  
 
 Post subject: code and stack trace
PostPosted: Thu Sep 08, 2005 11:50 am 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
I unfortuantely work on an off line system so need to type everything freehand.

I split the code into two methods but same result.

public boolean changeUserPasswords(String userSid, String oldPassword, String new Password) throws InfrastructureException
{
Session session = HibernateUtil.getSession();
Transaction tx = null;
analyst appUsers = null;
Long anID;
boolean exists = true;
AnalsytDAO anDAO = new AnalystDAO();
PasswordDAO pwDAO = new PasswordDAO();

if(oldPassword.equals(newPassword)
{
return exists;
}

try
{
tx = session.beginTransaction();
appUsers = anDAO.findBySids(session,userSid);
anID = appUsers.getAnalystID();
exists = pwDAO.usedPassword(session, anID, newPassword);
if (exists)
{
tx.commit();
return exists;
}
addPasswordHistory(appUsers, newPassword);

Date dte = Calendar.getInstance().getTime();
appUsers.setPassword(newPassword);
appUsers.setPwdCreationDate(dte);
anDAO.makePersistent(session, appUsers);
exists = false;
tx.commit();
return exists;
}
catch (HibernateException ex)
{
if(tx != null)
{
tx.rollback();
}
throw new InfrastructureException(ex);
}
finally
{
session.close();
}
return exists;
}


public addPasswordHistory(Analyst appUsers, String newPassword) throws IE
{
Session session = HibernateUtil.getSession();
PasswordHistory history = new PasswordHistory();
PasswordDAO pwDAO = new PasswordDAO();
Transaction tx = null;
Long anID;
Try
{
anID = appUsers.getAnalsytID();
tx = session.beginTransacation();
history.setAnalystID(anID);
history.setPassword(newPassword);
history.setPwdCreationDate(appUsers.getPwdCreationDate());
pwDAO.makePersistent(session, history);
tx.commit();
}
catch ….
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 8:21 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
Are you caching PasswordHistory beans inside PasswordDAO? I can't think of any other reason for your error...

Hope it helps.
Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


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