-->
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.  [ 6 posts ] 
Author Message
 Post subject: save or update of object with assigned id - not working
PostPosted: Thu Oct 16, 2003 5:36 am 
Newbie

Joined: Sun Sep 07, 2003 8:46 pm
Posts: 10
I am using Hibernate 2.03 and when I use the following code the objects are not being persisted. I have read the docs and this seems to be the way it should be done.

There is no exception. The mapping and log are below. Please assist

public void updateww(List wwscores) throws HibernateException{
Session s = null;
Transaction tx = null;
try{
s = Data.getInstance().getSession();
tx = s.beginTransaction();

for (int i = 0; i < wwscores.size(); i++) {

IWWScore score = (IWWScore)wwscores.get(i);
Wwscore wwscore = (Wwscore) s.load(Wwscore.class, score.getAppno());
boolean saved = false;
if (wwscore == null){
saved = true;
wwscore = new Wwscore();
wwscore.setAppno(score.getAppno());
}
wwscore.setWwone(score.getWwone());
wwscore.setWwtwo(score.getWwtwo());
wwscore.setWwthree(score.getWwthree());
wwscore.setWwfour(score.getWwfour());
wwscore.setWwfive(score.getWwfive());
wwscore.setWwsix(score.getWwsix());
wwscore.setWwseven(score.getWwseven());
wwscore.setWweight(score.getWweight());
wwscore.setWwsummary(score.getWwsummary());
if (saved)
s.update(wwscore);
else
s.save(wwscore);
logger.info("Update of Candidate " + wwscore.getAppno());
}
tx.commit();
}catch (HibernateException e){
logException(logger, e, BaseDAO.LOG_ERROR);
if (tx != null){
tx.rollback();
}
throw e;
}finally{
s.close();
}
}

<hibernate-mapping>
<class name="ocao.wads.model.business.Wwscore" table="WWSCORE" schema="ADMISS">
<id name="appno" column="APPNO" type="java.lang.Long" unsaved-value="null">
<generator class="assigned"/>
</id>
<property name="wwone" column="WWONE" type="java.lang.String" length="5"/>
<property name="wwtwo" column="WWTWO" type="java.lang.String" length="5"/>
<property name="wwthree" column="WWTHREE" type="java.lang.String" length="5"/>
<property name="wwfour" column="WWFOUR" type="java.lang.String" length="5"/>
<property name="wwsummary" column="WWSUMMARY" type="java.lang.String" length="5"/>
<property name="wwfive" column="WWFIVE" type="java.lang.String" length="5"/>
<property name="wwsix" column="WWSIX" type="java.lang.String" length="5"/>
<property name="wwseven" column="WWSEVEN" type="java.lang.String" length="5"/>
<property name="wweight" column="WWEIGHT" type="java.lang.String" length="5"/>
</class>
</hibernate-mapping>

DEBUG - returning cached statement: select wwscore0_.APPNO as APPNO, wwscore0_.WWONE as WWONE, wwscore0_.WWTWO as WWTWO, wwscore0_.WWTHREE as WWTHREE, wwscore0_.WWFOUR as WWFOUR, wwscore0_.WWSUMMARY as WWSUMMARY, wwscore0_.WWFIVE as WWFIVE, wwscore0_.WWSIX as WWSIX, wwscore0_.WWSEVEN as WWSEVEN, wwscore0_.WWEIGHT as WWEIGHT from ADMISS.WWSCORE wwscore0_ where wwscore0_.APPNO=?
DEBUG - binding '201033' to parameter: 1
DEBUG - processing result set
DEBUG - result row: 201033
DEBUG - Initializing object from ResultSet: 201033
DEBUG - Hydrating entity: ocao.wads.model.business.Wwscore#201033
DEBUG - returning null as column: WWONE
DEBUG - returning null as column: WWTWO
DEBUG - returning null as column: WWTHREE
DEBUG - returning null as column: WWFOUR
DEBUG - returning null as column: WWSUMMARY
DEBUG - returning null as column: WWFIVE
DEBUG - returning null as column: WWSIX
DEBUG - returning null as column: WWSEVEN
DEBUG - returning null as column: WWEIGHT
DEBUG - done processing result set (1 rows)
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - recaching
DEBUG - total checked-out statements: 0
DEBUG - checked out: []
DEBUG - total objects hydrated: 1
DEBUG - resolving associations for [ocao.wads.model.business.Wwscore#201033]
DEBUG - done materializing entity [ocao.wads.model.business.Wwscore#201033]
DEBUG - object already associated with session
INFO - Update of Candidate 201033
DEBUG - commit
DEBUG - flushing session
DEBUG - Flushing entities and processing referenced collections
DEBUG - Processing unreferenced collections
DEBUG - Scheduling collection removes/(re)creates/updates
DEBUG - Flushed: 0 insertions, 0 updates, 0 deletions to 41 objects
DEBUG - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG - executing flush
DEBUG - post flush
DEBUG - transaction completion
DEBUG - closing session
DEBUG - disconnecting session
DEBUG - returning connection to pool, pool size: 2
DEBUG - transaction completion
[Finalizer] DEBUG - running Session.finalize()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 5:56 pm 
Newbie

Joined: Sun Sep 07, 2003 8:46 pm
Posts: 10
anyone listening?


Top
 Profile  
 
 Post subject: Re: save or update of object with assigned id - not working
PostPosted: Fri Oct 17, 2003 9:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
From the session.load javadoc
Quote:
You should not use this method to determine if an instance exists (use find() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.


qasim wrote:
if (saved)
s.update(wwscore);
else
s.save(wwscore);
logger.info("Update of Candidate " + wwscore.getAppno());


Will always log "Update of Candidate " + wwscore.getAppno() because of {} missing.

If you load an object from a session, you don't have to call session.update. Hibernate knows when it is updated and will flush it.

Try this kind of code
Code:
List list = session.find("myrequest");
if (list.size() == 0) {
  Object obj = new Object();
  // fill object including id
  session.save(obj);
} else {
  Object obj = (Object) list.get(0);
  //fill non id params
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 1:02 pm 
Newbie

Joined: Sun Sep 07, 2003 8:46 pm
Posts: 10
I used find no problems there but there is no update to the objects state which was the problem that I was having. There are no errors. As you can see from the log it sees 0 updates

DEBUG - commit
DEBUG - flushing session
DEBUG - Flushing entities and processing referenced collections
DEBUG - Processing unreferenced collections
DEBUG - Scheduling collection removes/(re)creates/updates
DEBUG - Flushed: 0 insertions, 0 updates, 0 deletions to 41 objects
DEBUG - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG - executing flush
DEBUG - post flush
DEBUG - transaction completion
DEBUG - closing session


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 3:51 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do you update with different values ?
I saw in your first logs that null values are retruned by DB.

I'm not sure of me but Hibernate don't update your object if no real / different update is done.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 8:32 pm 
Newbie

Joined: Sun Sep 07, 2003 8:46 pm
Posts: 10
Sorry for wasting your time, this is working fine the problem was on the struts side and the fact that the values coming back were exactly the same.

Thanks for the help


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