Hi,
i just noticed a bug in Hibernate. It seems that Hibernate cannot handle native SQL queries which contain 2 (or more) columns
that have the same name (and different aliases).
Here the example db:
Code:
CREATE TABLE IF NOT EXISTS `tab1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `tab1` (`id`, `title`) VALUES
(1, 'tab1');
CREATE TABLE IF NOT EXISTS `tab2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `tab2` (`id`, `title`) VALUES
(1, 'tab2');
And the example code:
Code:
Session session = Hibernate.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Map<String, Object>> result = session.createSQLQuery("select tab1.title, tab2.title from tab1, tab2 where tab1.id = tab2.id")
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
.list();
for (Map<String, Object> row : result) {
System.out.println(row);
}
session.getTransaction().commit();
The output is:
Code:
{title=tab1}
I expect something like (or an error because of non unique columns):
Code:
{tab1title=tab1, tab2title=tab2}
If i change the query to:
Code:
select tab1.title as title1, tab2.title as title2 from tab1, tab2 where tab1.id = tab2.id
I get an error:
Code:
ERROR org.hibernate.util.JDBCExceptionReporter - Column 'title' not found.
Expected:
Code:
{title1=tab1, title2=tab2}
Using no ResultTransformer or the TO_LIST Transformer doesn't work either.
Is there any fix or workaround for this bug?
Thanks a lot!
Yours
Thomas W
Edit:
I am using Hibernate 3.5.3-Final
Edit2:
Just tried Hibernate 3.6.0-Final... same problem