-->
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: criteria.list returning more than one record on PK ???
PostPosted: Thu Sep 22, 2005 12:15 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Hi

Hibernate version:2.1

Code between sessionFactory.openSession() and session.close():

Session session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria( A.class );
criteria.add( Expression.eq( "aId", new Long("15")) );
finds = criteria.list();
HibernateUtil.closeSession();

I have a table A with aId as primary key. When I search on primary key = 15, I get more than one record back. Is it because

1. It is possible to have more than one instance of that record in the session
2. I am not using transaction and thats why Hibernate is not synchronizing session with the database before query

I believe if I use uniqueResult() method instead, I will still get the same problem with exception thrown. If I use following on criteria object which might solve the problem
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

but I would like to know why criteria.list() is returning more than one record back when database has only one ?

Thanks in advance.
Ron


Top
 Profile  
 
 Post subject: Re: criteria.list returning more than one record on PK ???
PostPosted: Fri Sep 23, 2005 1:44 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
rrr123 wrote:
I have a table A with aId as primary key. When I search on primary key = 15, I get more than one record back. Is it because

1. It is possible to have more than one instance of that record in the session
2. I am not using transaction and thats why Hibernate is not synchronizing session with the database before query

Somehow I can't believe what you're writing. If Hibernate behaved like that it would long be dead.

Could you please post a bit more concrete code and mapping so that I can try to reproduce that behavior?

Erik


Top
 Profile  
 
 Post subject: criteria.list returning more than one record for PK ...
PostPosted: Fri Sep 23, 2005 9:11 am 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Hi Erik

Thanks for replying. Unfortunately, Hibernate returns more than one record back for a primary key ... I solved this problem by
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

This is happened in other instances in my project also. It could be a bug
in 2.1. Please note that I have been using Hibernate for one year now.

Thanks
Ron

Here is the code:
---------------------------
try {
Session session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria( ActionPlan.class );

criteria.add( Expression.eq( "actionPlanId", key ) );
finds = criteria.list();

if ( finds.size() > 1 ) {
throw new DAOException( "There is currently more than one active record in the system with id [" + key + "]");
}
if ( finds.size() < 1 ) {
throw new DAOException( "There is currently no active record in the system with id [" + key + "]");
}
} catch ( HibernateException he ) {
String message = "Failed to perform findByPkDeepReview for [ActionPlan]";
log.error( message, he );
throw new DAOException( message , he );
} finally {
HibernateUtil.closeSession();
}
-------------------------------
<?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>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class name="gov.hud.cpd.drgr.business.ActionPlan" table="ACTION_PLAN" >
<meta attribute="implement-equals" inherit="false">true</meta>

<id name="actionPlanId" type="java.lang.Long" column="ACTION_PLAN_ID" >
<meta attribute="use-in-equals">true</meta>
<generator class="sequence">
<param name="sequence">ACTION_PLAN_ID</param>
</generator>
</id>

<property name="planDescription" type="java.lang.String" column="PLAN_DESCRIPTION" length="4000" >
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
</property>

<property name="hqDispositionDate" type="java.sql.Timestamp" column="HQ_DISPOSITION_DATE" length="7" >
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
</property>

<property name="insertTimestamp" type="java.sql.Timestamp" column="INSERT_TIMESTAMP" length="7" >
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
</property>

<property name="lastUpdateTimestamp" type="java.sql.Timestamp" column="LAST_UPDATE_TIMESTAMP" length="7" >
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
</property>

<property name="rowStatusId" type="java.lang.Long" column="ROW_STATUS_ID" length="15" >
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
</property>

<!-- Associations -->

<!-- bi-directional one-to-many association to Activity -->
<set name="activities" lazy="true" inverse="true" cascade="save-update" >
<key column="ACTION_PLAN_ID" />
<one-to-many class="gov.hud.cpd.drgr.business.Activity" />
</set>

<!-- bi-directional one-to-many association to ActionPlanHist -->
<set name="actionPlanHists" lazy="true" inverse="true" cascade="all-delete-orphan" >
<key column="ACTION_PLAN_ID" />
<one-to-many class="gov.hud.cpd.drgr.business.ActionPlanHist" />
</set>

<!-- bi-directional one-to-many association to Qpr -->
<list name="qprs" lazy="true" inverse="true" cascade="none" >
<key column="ACTION_PLAN_ID" />
<index column="POSITION"/>
<one-to-many class="gov.hud.cpd.drgr.business.Qpr" />
</list>

<!-- bi-directional one-to-many association to ActionPlanFunding -->
<set name="actionPlanFundings" lazy="true" inverse="true" cascade="all-delete-orphan" >
<key column="ACTION_PLAN_ID" />
<one-to-many class="gov.hud.cpd.drgr.business.ActionPlanFunding" />
</set>

<list name="actionPlanNeeds" lazy="false" table="ACTION_PLAN_NEED" outer-join="true" >
<key column="ACTION_PLAN_ID" />
<index column="POSITION"/>
<element type="string" column="NEED_DESCRIPTION" not-null="true" />
</list>

<!-- bi-directional one-to-many association to ActionPlanReviewHist -->
<set name="actionPlanReviewHists" lazy="true" inverse="true" cascade="all-delete-orphan" order-by="REVIEW_DATE" >
<key column="ACTION_PLAN_ID" />
<one-to-many class="gov.hud.cpd.drgr.business.ActionPlanReviewHist" />
</set>

<!-- bi-directional many-to-one association to Grant -->
<many-to-one name="grant" class="gov.hud.cpd.drgr.business.Grant" not-null="true" >
<meta attribute="use-in-equals">true</meta>
<column name="GRANTS_ID" />
</many-to-one>

<many-to-one name="reviewStatus" column="REVIEW_STATUS_ID" class="gov.hud.cpd.drgr.business.ReviewStatus" not-null="true" />

<!-- bi-directional many-to-one association to Grantee -->

<many-to-one name="grantee" class="gov.hud.cpd.drgr.business.Grantee" not-null="true" >
<meta attribute="use-in-equals">true</meta>
<column name="GRANTEE_ID" />
</many-to-one>

<!-- bi-directional many-to-one association to User -->
<many-to-one name="userByLastUpdateUserId" class="gov.hud.cpd.drgr.business.User" not-null="true" >
<meta attribute="use-in-equals">true</meta>
<column name="LAST_UPDATE_USER_ID" />
</many-to-one>

<!-- bi-directional many-to-one association to User -->
<many-to-one name="userByInsertUserId" class="gov.hud.cpd.drgr.business.User" not-null="true" >
<meta attribute="use-in-equals">true</meta>
<column name="INSERT_USER_ID" />
</many-to-one>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 11:55 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Hi Ron,

I tried to reproduce the problem but failed. Without a self contained, unit test like example of the problem, I may spend days trying around without success - and I don't have that much time...

Let's hope one of the Hibernate developer has an explanation for your issue.

The explanations you suggest are not really satisfactory as Hibernate should normally throw a NonUniqueObjectException if the same identifier was associated more than once to a given session. I've been wondering whether this could be some subtle issue with the implementation of equals - but that's only a wild guess.

Sorry I couldn't help you.

Erik


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.