-->
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: Multiple updates to a table using SAVEORUPDATE of DAO.
PostPosted: Sat Oct 21, 2006 9:05 pm 
Newbie

Joined: Tue Oct 17, 2006 10:57 am
Posts: 14
I am using saveorupdate(records) method of Hibernate template where records is a List object containing n number of records of ACCOUNT ( domain object ).

The account records are retrieved and shown to the client and they can update some or none of the records. Now my question is does hibernate update all records irrespective of whether user changed it or not on the scr
een ?

also whats the diff between save() and update() ?

Thanks...sorry for my novice question ..I am new to hibernate .


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 22, 2006 12:57 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
If you want to update only those records which have changed, use
Code:
dynamic-update="true"
in your hbm file(Have a look at the hibernate dtd for more info).

Quote:
also whats the diff between save() and update() ?


Save considers the passed object to be a new one and fires a insert query, whereas update considers the object to be already existing and fires a update query.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 22, 2006 2:01 am 
Newbie

Joined: Tue Oct 17, 2006 10:57 am
Posts: 14
Thanks jaikiran. I tried it but still it runs updates on all records retrieved from db . I am using getHibernateTemplate().execute(callback) to retrive records.

The records are shown to user in table in IE. I am using SPRING:bind to bind the one updateable field to domain object property.

Even if the user updates one row in IE and hits SAVE I can see tha hibernate in backend updates all records and increments the version no by 1 on all ( thats how i know updates ran on all ) and also the SQL log in console shows UPDATES running on all records.

and I am using getHibernateTemplate().saveOrUpdate(list) to run updates.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 22, 2006 3:32 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Please post your mapping files and the queries that are being fired by hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 22, 2006 12:30 pm 
Newbie

Joined: Tue Oct 17, 2006 10:57 am
Posts: 14
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.wmg.digital.esdc.freeflag.domain" default-lazy="true">

<class
name="FreeFlagError"
table="ESDC_SUSPENSE_QUEUE"
dynamic-update="true"
mutable="true"
>
<cache usage="read-write"/>

<id
name="id"
type="long"
column="ID"
>
<generator class="sequence">
<param name="sequence">ESDC_QUEUE_ID_SEQ</param>
</generator>
</id>

<version
name="version"
type="long"
column="VERSION"
>
</version>
<property
name="errorId"
type="long"
column="ERROR_ID"
not-null="true"
>
</property>

<property
name="detailId"
type="long"
column="DETAIL_ID"
not-null="true"
>
</property>

<property
name="statusId"
type="long"
column="STATUS_ID"
not-null="true"
>
</property>

<property
name="dealerAccountId"
type="long"
column="DEALER_ACCOUNT_ID"
not-null="false"
>
</property>

<property
name="units"
type="long"
column="UNITS"
not-null="false"
>
</property>

<property
name="dsp"
type="string"
column="DSP"
not-null="false"
length="255"
>
</property>

<property
name="artist"
type="string"
column="REPORTED_ARTIST"
not-null="false"
length="255"
>
</property>

<property
name="title"
type="string"
column="REPORTED_TITLE"
not-null="false"
length="255"
>
</property>
<property
name="primaryProductIdentifier"
type="string"
column="PRIMARY_PRODUCT_IDENTIFIER"
not-null="false"
length="100"
>
</property>

<property
name="freeFlag"
type="yes_no"
column="FREE_FLAG"
not-null="false"
>
</property>

<property
name="transactionDate"
type="date"
column="TRANSACTION_DATE"
not-null="false"
>
</property>

<property
name="lastUpdateUser"
type="string"
column="LAST_UPDATE_USER"
not-null="false"
length="25"
>
</property>


</class>

</hibernate-mapping>



package com.wmg.digital.esdc.freeflag.dao.impl.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wmg.core.springframework.orm.hibernate3.GeneralCriteriaCallback;
import com.wmg.core.springframework.orm.hibernate3.UniqueCriteriaCallback;
import com.wmg.digital.esdc.freeflag.dao.FreeFlagErrorDao;
import com.wmg.digital.esdc.freeflag.domain.FreeFlagError;
import com.wmg.digital.esdc.freeflag.domain.Error;


public class FreeFlagErrorImpl extends HibernateDaoSupport implements FreeFlagErrorDao {

@SuppressWarnings("unchecked")
public List<FreeFlagError> findAllFreeFlagError() {

GeneralCriteriaCallback callback = new GeneralCriteriaCallback(
FreeFlagError.class);
callback.addFetchMode("freeflagerror", FetchMode.SELECT);
callback.addExpression(Restrictions.eq("errorId", Error.FREE_FLAG_ERROR_ID));
callback.addExpression(Restrictions.ne("statusId", Error.STATUS_ID_PROCESSED));
callback.addOrderBy(Order.desc("units"));

List<FreeFlagError> ffe = (List<FreeFlagError>) getHibernateTemplate()
.execute(callback);


return ffe;
}

public List<FreeFlagError> findTop10FreeFlagError() {

GeneralCriteriaCallback callback = new GeneralCriteriaCallback(
FreeFlagError.class);
callback.addFetchMode("freeflagerror", FetchMode.SELECT);
callback.addExpression(Restrictions.eq("errorId", Error.FREE_FLAG_ERROR_ID));
callback.addExpression(Restrictions.ne("statusId", Error.STATUS_ID_PROCESSED));
callback.addOrderBy(Order.desc("units"));
callback.setMaxResults(10);

List<FreeFlagError> ffe = (List<FreeFlagError>) getHibernateTemplate()
.execute(callback);


return ffe;
}

public List<FreeFlagError> findFreeFlagErrorByDSP(String pattern) {
String newPattern = StringUtils.replace(pattern, "*", "%");
GeneralCriteriaCallback callback = new GeneralCriteriaCallback(
FreeFlagError.class);
callback.addFetchMode("freeflagerror", FetchMode.SELECT);
callback.addExpression(Restrictions.eq("errorId", Error.FREE_FLAG_ERROR_ID));
callback.addExpression(Restrictions.ne("statusId", Error.STATUS_ID_PROCESSED));
if (StringUtils.isNotEmpty(newPattern)) {
callback.addExpression(Restrictions.like("dsp", newPattern)
.ignoreCase());
}
callback.addOrderBy(Order.desc("units"));
List<FreeFlagError> ffe = (List<FreeFlagError>) getHibernateTemplate().execute(callback);


return ffe;

}

public List<FreeFlagError> findFreeFlagErrorByArtist(String pattern) {
String newPattern = StringUtils.replace(pattern, "*", "%");
GeneralCriteriaCallback callback = new GeneralCriteriaCallback(
FreeFlagError.class);
callback.addFetchMode("freeflagerror", FetchMode.SELECT);
callback.addExpression(Restrictions.eq("errorId", Error.FREE_FLAG_ERROR_ID));
callback.addExpression(Restrictions.ne("statusId", Error.STATUS_ID_PROCESSED));
if (StringUtils.isNotEmpty(newPattern)) {
callback.addExpression(Restrictions.like("artist", newPattern)
.ignoreCase());
}
callback.addOrderBy(Order.desc("units"));
List<FreeFlagError> ffe = (List<FreeFlagError>) getHibernateTemplate().execute(callback);


return ffe;

}

public List<FreeFlagError> findFreeFlagErrorByTitle(String pattern) {
String newPattern = StringUtils.replace(pattern, "*", "%");
GeneralCriteriaCallback callback = new GeneralCriteriaCallback(
FreeFlagError.class);
callback.addFetchMode("freeflagerror", FetchMode.SELECT);
callback.addExpression(Restrictions.eq("errorId", Error.FREE_FLAG_ERROR_ID));
callback.addExpression(Restrictions.ne("statusId", Error.STATUS_ID_PROCESSED));
if (StringUtils.isNotEmpty(newPattern)) {
callback.addExpression(Restrictions.like("title", newPattern)
.ignoreCase());
}
callback.addOrderBy(Order.desc("units"));
List<FreeFlagError> ffe = (List<FreeFlagError>) getHibernateTemplate().execute(callback);


return ffe;

}

public FreeFlagError getFreeFlagErrorById(Long ffid) {

UniqueCriteriaCallback callback = new UniqueCriteriaCallback(
FreeFlagError.class);
callback.addExpression(Restrictions.eq("id", ffid));
FreeFlagError ffe = (FreeFlagError) getHibernateTemplate().execute(callback);

return ffe;

}

public void updateFfe(FreeFlagError ffe) {
getHibernateTemplate().update(ffe);
}

public void updateAllFfes(List<FreeFlagError> ffes) {
try {
for (FreeFlagError ffe : ffes) {
getHibernateTemplate().saveOrUpdate(ffe);
}
} catch (Exception e) {
System.err.println(e);
}
}


}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 22, 2006 12:55 pm 
Newbie

Joined: Tue Oct 17, 2006 10:57 am
Posts: 14
also I read in the documentation that setting DYNAMIC-UPDATE="TRUE" updates only the COLUMNS that changed. It has nothing to do with anything at the RECORDS level which is what I want.

So i used select-before-update="true" and that updates only the records which are being updated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 23, 2006 8:44 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Sorry, i was wrong. Got confused between the two. You have got it right.


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.