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> </td><td>2006-03-01 03:50:01.0</td><td>1</td><td>DENV</td><td>7.5</td><td>false</td><td> </td><td> </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> </td><td>2006-03-01 04:35:00.0</td><td>2</td><td>DENV</td><td>6.5</td><td> </td><td> </td><td> </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> </td><td>2006-03-01 16:35:00.0</td><td>4</td><td>DENV</td><td>6.5</td><td> </td><td> </td><td> </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> </td><td>2006-07-10 23:35:00.0</td><td>4</td><td>DENV</td><td>1.35</td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td>5</td><td>1</td><td>2006-07-10 18:00:00.0</td><td>56789</td><td> </td><td>2006-07-10 23:55:00.0</td><td>1</td><td>DENV</td><td>6.5</td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td>6</td><td>1</td><td>2006-01-16 12:00:00.0</td><td>9876</td><td> </td><td>2006-01-16 19:00:00.0</td><td>3</td><td>MWDC</td><td>7.0</td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td>7</td><td>0</td><td>2006-02-28 20:20:00.0</td><td>1536441727</td><td> </td><td>2006-03-01 03:50:01.0</td><td>1</td><td>DENV</td><td>24.0</td><td>false</td><td> </td><td> </td><td>true</td></tr>
</table>
Thanks for any help.
Tim