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.