-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Returns Multiple Similar Objects
PostPosted: Wed Oct 18, 2006 1:07 pm 
Newbie

Joined: Wed Oct 18, 2006 12:44 pm
Posts: 3
I am using Hibernate 3.1.3 with MySQL 5.0. I am analyzing Hibernate for a project and got into this strange problem. First let me tell you about the problem:

I created a table LOOKUP
Code:
CREATE TABLE `lookup`
(                 
          `templateId` varchar(40) NOT NULL,     
          `fieldName` varchar(40) default NULL, 
          `rowId` varchar(40) NOT NULL,         
          PRIMARY KEY  (`templateId`,`rowId`)   
) ENGINE=MyISAM DEFAULT CHARSET=utf8 


I created a JavaBean for this Lookup.java:
Code:
public class Lookup
{
   private String templateId;

   private String fieldName;

   private String rowId;

        //... getter/setter methods
}


Then created a Hibernate mapping file:
Code:
    <class name="test.Lookup" table="LOOKUP">
        <id name="templateId" column="TEMPLATEID" />
        <property name="fieldName" />
        <property name="rowId" />
    </class>


It's very simple but when I run the following query:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction session.beginTransaction();

List results = session.createQuery("from Lookup").list();     
session.getTransaction().commit();
HibernateUtil.closeSession();


The list of results returned contains 6 rows which is correct but all with same data which is incorrect, because they should be different. Hibernate generates following query for this task:
Code:
select lookup0_.TEMPLATEID as TEMPLATEID, lookup0_.fieldName as fieldName0_, lookup0_.rowId as rowId3_0_ from LOOKUP lookup0_

If I execute this query directly in the database, well then it returns the correct results, similarly if I just run the SQL query it gives me the correct results though in form of arrays which I don't want. The moment I add the .addEntity() code to SQLQuery it also starts returning the wrong result:
Code:
List results = session.createSQLQuery("Select {lookup.*} from Lookup").addEntity("lookup", Lookup.class).list();


So please anyone out there who can help do post a reply, because I tried debugging the Hibernate code for 4 hours or so, and in the end found that it is saving the correct results in properties but when I call getter methods of JavaBeans they just all return one result.

_________________
--
Regards,

Adeel Javed,
Software Engineer,
Techlogix Pakistan (Pvt) Limited.

Web: http://pkblogs.com/adeeljaved


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 1:34 pm 
Newbie

Joined: Wed Oct 18, 2006 1:28 pm
Posts: 3
I was having problems with duplicate results, and this solved my problems.
Try making:
Code:
List results = session.createQuery("from Lookup").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();


Hope I could help.


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