-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate Query returning the same rows
PostPosted: Mon Jan 08, 2007 5:28 pm 
Newbie

Joined: Mon Jan 08, 2007 5:17 pm
Posts: 3
I am newbie to Hibernate.

I am following hibernate mapping

<hibernate-mapping>
<class name="com.amp.rfq.AltPartView" table="MYTEST_BA_ALT_PART_V">

<id name="id" column="RFQ_ID">
<generator class="native"/>
</id>
<property name="combinationId" column="COMBINATION_ID"/>
<property name="altPartNo" column="PART_NO"/>
<property name="partNo" column="ALT_PART_NO"/>
<property name="mfg" column="MFG"/>
<property name="genPartNo" column="GEN_PART_NO"/>
</class>

</hibernate-mapping>

and the data in the datanase table looks something like

RFQ_ID,COMBINATION_ID,PART_NO,ALT_PART_NO,MFG,GEN_PART_NO
457177,775107,LM339ADT,LM339AD,STM
457177,775109,LM339ADT,LM339AD,ONS
457177,775110,LM339ADT,LM339ADR,ONS

When I use following code to get these three rows

List result = HibernateUtil.getSessionFactory()
.getCurrentSession().createCriteria(AltPartView.class)
.add(Restrictions.eq("id", Long.valueOf(457177)))
.list();

out.println("<h2>RFQ Id "+ rfqid +" Size is "+result.size()+" list "+result+" </h2>");
if (result.size() > 0) {
out.println("<h2>Alternates in database:</h2>");
out.println("<table border='1'>");
out.println("<tr>");
out.println("<th>Id</th>");
out.println("<th>Combination No</th>");
out.println("<th>Part No</th>");
out.println("<th>Alt Part No</th>");
out.println("<th>Mfg</th>");
out.println("<th>Gen Part No</th>");
out.println("</tr>");
for (Iterator it = result.iterator(); it.hasNext();) {
AltPartView altPartView = (AltPartView) it.next();
out.println("<tr>");
out.println("<td>" + altPartView+ "</td>");
out.println("<td>" + altPartView.getCombinationId()+ "</td>");
out.println("<td>" + altPartView.getPartNo()+ "</td>");
out.println("<td>" + altPartView.getAltPartNo() + "</td>");
out.println("<td>" + altPartView.getMfg() + "</td>");
out.println("<td>" + altPartView.getGenPartNo() + "</td>");
out.println("</tr>");
}
out.println("</table>");
}

I get following result

RFQ Id 457177 Size is 3 list [com.amp.rfq.AltPartView@14a75bb, com.amp.rfq.AltPartView@14a75bb, com.amp.rfq.AltPartView@14a75bb]

Alternates in database:

Id Combination No Part No Alt Part No Mfg Gen Part No
com.amp.rfq.AltPartView@14a75bb 775107 LM339AD LM339ADT STM null
com.amp.rfq.AltPartView@14a75bb 775107 LM339AD LM339ADT STM null
com.amp.rfq.AltPartView@14a75bb 775107 LM339AD LM339ADT STM null

I get first row for all three records. I am struggling with this for some time now without any success. Could somebody please help me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 5:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
RFQ_ID is obviously not the id in your data so don't map it as such ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 6:12 pm 
Newbie

Joined: Mon Jan 08, 2007 5:17 pm
Posts: 3
But RFQ_ID the primary key in this case. There is no other id on this table. What would be the mapping for id in hibernate-mapping in that situation


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 8:40 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
Trying for more detailed explanation...

It looks like the primary key could be the COMBINATION_ID, your
RFQ_ID is definitely ambiguous - it is not a primary key.

I suspect Hibernate object identity is somewhat confused here - the posting does not show implementation of the equals() operator
(read Hibernate book session on object identity).

It looks like telling Hibernate that RFQ_ID is the primary key makes Hibernate believe that once it loaded the first row with RFQ_ID=value,
all the remaining rows ar 'identical' (having the same primary key) and hence it returns the same (first row) object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 11:11 pm 
Newbie

Joined: Mon Jan 08, 2007 5:17 pm
Posts: 3
Your are right. After more study of the Table structure itself(Actually it is a View not a table) I found that the unique id is RFQ_ID + COMBINATION_ID. So I changed the hibernate mapping to use composite-id instead of id and defined a new class for that composite-id and that solved the problem. Anyway thanks a lot for all your help.


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