-->
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.  [ 1 post ] 
Author Message
 Post subject: [Bug] Hibernate cannot do native SQL with identical columns
PostPosted: Wed Nov 03, 2010 10:01 am 
Beginner
Beginner

Joined: Sun Apr 25, 2010 7:14 am
Posts: 30
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.