-->
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.  [ 4 posts ] 
Author Message
 Post subject: HQL update query problem
PostPosted: Fri Apr 07, 2006 3:08 pm 
Newbie

Joined: Tue Mar 15, 2005 7:09 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
<?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="com.cardinal.dst.model.CahLoadAssignment"
table="dds_loadassignment"
>

<id
name="id"
column="load_asgnmt_id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CahLoadAssignment.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<version
name="version"
column="version"
type="java.lang.Integer"
/>

<property
name="loadStartDate"
type="java.util.Date"
update="true"
insert="true"
column="loadStartDate"
not-null="true"
/>

<property
name="loadId"
type="java.lang.Long"
update="true"
insert="true"
column="loadId"
not-null="true"
/>

<property
name="tripId"
type="java.lang.Long"
update="true"
insert="true"
column="tripId"
not-null="false"
/>

<property
name="loadCompletionDate"
type="java.util.Date"
update="true"
insert="true"
column="loadCompletionDate"
not-null="true"
/>

<many-to-one
name="driver"
class="com.cardinal.dst.model.CahDriver"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="driver_id"
not-null="false"
/>

<many-to-one
name="region"
class="com.cardinal.dst.model.CahRegion"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="reg_name"
not-null="true"
/>

<property
name="loadDuration"
type="java.lang.Float"
update="true"
insert="true"
column="loadDuration"
not-null="true"
/>

<property
name="unassigned"
type="java.lang.Boolean"
update="true"
insert="true"
column="unassigned"
/>

<property
name="actualLoadDuration"
type="java.lang.Float"
update="true"
insert="true"
column="actualLoadDuration"
not-null="false"
/>

<property
name="loadStatus"
type="java.lang.Integer"
update="true"
insert="true"
column="loadStatus"
not-null="false"
/>

<property
name="validated"
type="java.lang.Boolean"
update="true"
insert="true"
column="validated"
not-null="false"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CahLoadAssignment.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


<?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="com.cardinal.dst.model.CahRegion"
table="dds_region"
lazy="false"
>

<id
name="name"
column="reg_name"
type="java.lang.String"
length="30"
unsaved-value="version"
>
<generator class="assigned">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CahRegion.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<version
name="version"
column="version"
type="java.lang.Integer"
/>

<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="reg_desc"
length="256"
/>

<set
name="serviceCodes"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
order-by="srvc_cd"
>

<key
column="reg_name"
>
</key>

<one-to-many
class="com.cardinal.dst.model.ServiceCode"
/>

</set>

<property
name="dotMinimumOffDutyHours"
type="java.lang.Float"
update="true"
insert="true"
column="dotMinimumOffDutyHours"
length="2"
not-null="false"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CahRegion.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
All access uses the classes CahRegion and CahLoadAssignment which correspond to the mapping docs above.
Using Spring session via Springs HibernateDaoSupport class. Here is the access code inside my DAO subclass:

Code:
private static final String updateValidationStatuses =
"update CahLoadAssignment set validated = :status " + "where region = :region " + "and loadStartDate >= :startDate " + "and loadStartDate < :endDate ";

   public int updateValidationStatuses(CahRegion region
                               ,Date startDate
                               ,Date endDate
                               ,boolean status) {
       
       int updatedCount = 0;
       
       if(region != null && startDate != null && endDate != null) {
          updatedCount = getSession()
                     .createQuery(updateValidationStatuses)
                     .setBoolean("status", status)
                     .setEntity("region", region)
                     .setDate("startDate", startDate)
                     .setDate("endDate", endDate)
                     .executeUpdate();
       }
       
       return updatedCount;
       
    }

public List getCahLoadAssignments(CahRegion region, Date startDate, Date endDate) {
     if(region != null && startDate != null && endDate != null) {
          List assignments = getSession().createCriteria(CahLoadAssignment.class)
                         .add(Restrictions.eq("region", region))
                         .add(Restrictions.ge("loadStartDate", startDate))
                         .add(Restrictions.lt("loadStartDate", endDate))
                         .list();
          return assignments;
       }
       else {
          return null;
       }
      }



Full stack trace of any exception that occurs: No exceptions. The update count is not what is expected.

Name and version of the database you are using: Occurring in MySQL 4.1

The generated SQL (show_sql=true):
See below.

Debug level Hibernate log excerpt:

I am running a JUnit test case that first calls the update method above and then the get method. The updated row count is less than the get row count. The dates are identical for both calls. It appears that the update HQL query is not updating all of the required rows. The get call is returning the correct row count.

Here is my JUnit output:

Logging output for passed in times:
Time start = Tue Feb 28 17:00:00 CST 2006
Time end = Wed Mar 01 17:00:00 CST 2006

Logging show_sql for update call:
Hibernate: update dds_loadassignment set validated=? where reg_name=
? and loadStartDate>=? and loadStartDate<?

Logging output for updated row count:
[junit] [dst] DEBUG [main] CahLoadAssignmentDAOTest.testUpdateValidationStat
uses(168) | Count = 3

Logging show_sql for get call:
[junit] Hibernate: select this_.load_asgnmt_id as load1_3_, this_.version as
version7_3_, this_.loadStartDate as loadStar3_7_3_, this_.loadId as loadId7_3_,
this_.tripId as tripId7_3_, this_.loadCompletionDate as loadComp6_7_3_, this_.d
river_id as driver7_7_3_, this_.reg_name as reg8_7_3_, this_.loadDuration as loa
dDura9_7_3_, this_.unassigned as unassigned7_3_, this_.actualLoadDuration as act
ualL11_7_3_, this_.loadStatus as loadStatus7_3_, this_.validated as validated7_3
_, cahdriver2_.driver_id as driver1_0_, cahdriver2_.version as version5_0_, cahd
river2_.driverExternalId as driverEx3_5_0_, cahdriver2_.driverEmployeeId as driv
erEm4_5_0_, cahdriver2_.firstName as firstName5_0_, cahdriver2_.lastName as last
Name5_0_, cahdriver2_.contactNumber as contactN7_5_0_, cahdriver2_.cellNumber as
cellNumber5_0_, cahdriver2_.employmentStartDate as employme9_5_0_, cahdriver2_.
information as informa10_5_0_, cahdriver2_.weeklyCommittedHours as weeklyC11_5_0
_, cahdriver2_.reg_name as reg12_5_0_, cahdriver2_.enabled as enabled5_0_, cahre
gion3_.reg_name as reg1_1_, cahregion3_.version as version8_1_, cahregion3_.reg_
desc as reg3_8_1_, cahregion3_.dotMinimumOffDutyHours as dotMinim4_8_1_, cahregi
on4_.reg_name as reg1_2_, cahregion4_.version as version8_2_, cahregion4_.reg_de
sc as reg3_8_2_, cahregion4_.dotMinimumOffDutyHours as dotMinim4_8_2_ from dds_l
oadassignment this_ left outer join dds_driver cahdriver2_ on this_.driver_id=ca
hdriver2_.driver_id left outer join dds_region cahregion3_ on cahdriver2_.reg_na
me=cahregion3_.reg_name inner join dds_region cahregion4_ on this_.reg_name=cahr
egion4_.reg_name where this_.reg_name=? and this_.loadStartDate>=? and this_.loa
dStartDate<?
[junit] Hibernate: select drivertype0_.driver_id as driver1_1_, drivertype0_
.type_code as type2_1_, drivertype1_.type_code as type1_0_, drivertype1_.version
as version9_0_, drivertype1_.description as descript3_9_0_ from dds_driver_driv
ertype drivertype0_ inner join dds_driver_type drivertype1_ on drivertype0_.type
_code=drivertype1_.type_code where drivertype0_.driver_id=? order by drivertype0
_.type_code
[junit] Hibernate: select drivertype0_.driver_id as driver1_1_, drivertype0_
.type_code as type2_1_, drivertype1_.type_code as type1_0_, drivertype1_.version
as version9_0_, drivertype1_.description as descript3_9_0_ from dds_driver_driv
ertype drivertype0_ inner join dds_driver_type drivertype1_ on drivertype0_.type
_code=drivertype1_.type_code where drivertype0_.driver_id=? order by drivertype0
_.type_code

Logging output for get row count:
[junit] [dst] DEBUG [main] CahLoadAssignmentDAOTest.testUpdateValidationStat
uses(175) | Result size = 4



Here is a snapshot of my DB records:
<table border=1><tr BGCOLOR="#CCCCFF"><th>load_asgnmt_id</th><th>version</th><th>loadStartDate</th><th>loadId</th><th>tripId</th><th>loadCompletionDate</th><th>driver_id</th><th>reg_name</th><th>loadDuration</th><th>unassigned</th><th>actualLoadDuration</th><th>loadStatus</th><th>validated</th></tr>
<tr><td>1</td><td>2</td><td>2006-02-28 20:20:00.0</td><td>12345</td><td>&nbsp;</td><td>2006-03-01 03:50:01.0</td><td>1</td><td>DENV</td><td>7.5</td><td>false</td><td>&nbsp;</td><td>&nbsp;</td><td>true</td></tr>
<tr><td>2</td><td>1</td><td>2006-02-28 23:00:00.0</td><td>54321</td><td>&nbsp;</td><td>2006-03-01 04:35:00.0</td><td>2</td><td>DENV</td><td>6.5</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>true</td></tr>
<tr><td>3</td><td>1</td><td>2006-03-01 08:00:00.0</td><td>56789</td><td>&nbsp;</td><td>2006-03-01 16:35:00.0</td><td>4</td><td>DENV</td><td>6.5</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>true</td></tr>
<tr><td>4</td><td>1</td><td>2006-07-10 22:00:00.0</td><td>98765</td><td>&nbsp;</td><td>2006-07-10 23:35:00.0</td><td>4</td><td>DENV</td><td>1.35</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>5</td><td>1</td><td>2006-07-10 18:00:00.0</td><td>56789</td><td>&nbsp;</td><td>2006-07-10 23:55:00.0</td><td>1</td><td>DENV</td><td>6.5</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>6</td><td>1</td><td>2006-01-16 12:00:00.0</td><td>9876</td><td>&nbsp;</td><td>2006-01-16 19:00:00.0</td><td>3</td><td>MWDC</td><td>7.0</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>7</td><td>0</td><td>2006-02-28 20:20:00.0</td><td>1536441727</td><td>&nbsp;</td><td>2006-03-01 03:50:01.0</td><td>1</td><td>DENV</td><td>24.0</td><td>false</td><td>&nbsp;</td><td>&nbsp;</td><td>true</td></tr>
</table>


Thanks for any help.

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 3:26 pm 
Newbie

Joined: Tue Mar 15, 2005 7:09 pm
Posts: 4
I forgot to attche the Hibernate config, via a Spring context file on my previous post:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/cardinal/dst/model/Role.hbm.xml</value>
<value>com/cardinal/dst/model/User.hbm.xml</value>
<value>com/cardinal/dst/model/UserCookie.hbm.xml</value>
<value>com/cardinal/dst/model/CahDriver.hbm.xml</value>
<value>com/cardinal/dst/model/CahLoadAssignment.hbm.xml</value>
<value>com/cardinal/dst/model/CahRegion.hbm.xml</value>
<value>com/cardinal/dst/model/DriverType.hbm.xml</value>
<value>com/cardinal/dst/model/Equipment.hbm.xml</value>
<value>com/cardinal/dst/model/EquipmentType.hbm.xml</value>
<value>com/cardinal/dst/model/ScheduleWorkDay.hbm.xml</value>
<value>com/cardinal/dst/model/ScheduleVacation.hbm.xml</value>
<value>com/cardinal/dst/model/ServiceCode.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- Create/update the database tables automatically when the JVM starts up
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
<!-- Turn batching off for better error messages under PostgreSQL
<prop key="hibernate.jdbc.batch_size">0</prop> -->
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> -->
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<!-- Echo all executed SQL to stdout -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>


Top
 Profile  
 
 Post subject: Still having problems with update
PostPosted: Mon Apr 17, 2006 12:26 pm 
Newbie

Joined: Tue Mar 15, 2005 7:09 pm
Posts: 4
i'm still having this problem. Is anyone using/have a working example of the Hibernate executeUpdate() method. I am seeing that the show_sql displays the update statement but the DB records are not being updated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 3:55 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Does your JUnit code begin and commit a transaction somewhere outside the DAO? If there is no transaction, data won't be written.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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