I will risk the wrath of the Etiquette Police by re-posting this
question with a slightly different twist. The original post was
buried as a response to a related topic and perhaps lost in the
shuffle. I am hoping a Hibernate team member has some insight.
The relevant (as I see it ) facts:
1) I have a legacy table with non-unique keys. It is
a composite key.
2) When I query that table with a given "key" I get back
the NUMBER of records I expect, ie. details.size() below
reports as expected.
3) The problem: As I iterate over the result list, the non-key column
(there is only one, 'description', per hbm below), is
identical for all of the returned records. It should not be. Each "detail"
has a different 'descripition.'
4) Note that I use 'use-in-equals' for 'description'
field so as to provide some mechanism for Hibernate
to distinguish between instances.
5) I see the same behavior whether I use a Criteria query
(per code below) or HQL with getCashHistoryDetailDescriptionsById
query via Spring Framework. The code which uses HQL/Spring appears
below for illustration purposes but is commented out.
6) I am currently running against as400/db2 and can't even really
reproduce this mysql because of its constraint on duplicate keys.
My hypothesis is that the correct records are read into the
cache (given details.size() is correct) but when I try to retrieve them
from the cache as I iterate, I get the same one each iteration.
Does anyone have any ideas on this? Am I missing something
fundamental?
Hibernate version: 2.6
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class
name="com.trustetc.model.CashHistoryDetailDescription"
table="TRDHISS" >
<meta attribute="class-description" inherit="false">
@hibernate.class
table="TRDHISS"
</meta>
<composite-id name="id"
class="com.trustetc.model.CashHistoryDetailDescriptionID" >
<meta attribute="field-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<meta attribute="use-in-equals">true</meta>
<key-property
name="accountNumber"
column="TAN"
type="int"
length="7"
>
<meta attribute="field-description">
@hibernate.property
column="TAN"
</meta>
</key-property>
<key-property
name="postDate"
column="PDATE"
type="int"
length="7"
>
<meta attribute="field-description">
@hibernate.property
column="PDATE"
</meta>
</key-property>
</key-property>
<key-property
name="userId"
column="USERID"
type="java.lang.String"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="USERID"
</meta>
</key-property>
<key-property
name="postTime"
column="PTIME"
type="int"
length="6"
>
<meta attribute="field-description">
@hibernate.property
column="PTIME"
</meta>
</key-property>
</composite-id>
<property
name="description"
column="DESC"
type="java.lang.String"
length="35"
>
<meta attribute="field-description">
@hibernate.property
column="DESC"
</meta>
<meta attribute="use-in-equals">true</meta>
</property>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->
</class>
<query name="allCashHistoryDetailDescription">
<![CDATA[from CashHistoryDetailDescription]]>
</query>
<query name="getCashHistoryDetailDescription">
<![CDATA[from CashHistoryDetailDescription c where c.id.accountNumber = :accountNumber order by c.id.postDate desc ]]>
</query>
<query name="getCashHistoryDetailDescriptionsById">
<![CDATA[from CashHistoryDetailDescription c where c.id.accountNumber = :accountNumber and c.id.userId = :user and c.id.postDate = :pdate and c.id.postTime = :ptime order by c.id.postDate desc ]]>
</query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session = null;
try {
SessionFactory sf = getSessionFactory();
session = sf.openSession();
details = session.createCriteria(CashHistoryDetailDescription.class)
.add( Expression.eq("id.accountNumber", tan) )
.add( Expression.eq("id.postDate", pdate))
.add( Expression.eq("id.postTime", ptime))
.add( Expression.eq("id.userId", user)).list();
/*
// Using Spring Framework support classes
details = getHibernateTemplate()
.findByNamedQueryAndNamedParam("getCashHistoryDetailDescriptionsById",
new String[] { "accountNumber", "user", "pdate", "ptime" },
new Object[] { tan, user, pdate, ptime });
*/
Iterator i = details.iterator();
while (i.hasNext()) {
CashHistoryDetailDescription d = (CashHistoryDetailDescription) i.next();
// getDescription reports the same for each "detail" It should NOT!
log.info("detail description = " + d.getDescription());
}
} catch (HibernateException e) {
e.printStackTrace();
}
finally {
try { if (session != null) session.close(); } catch (Exception e) {}
}
Name and version of the database you are using: AS400 DB2 5.2