-->
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: Bug in SQL Native Query with Hibernate 3.6.0 Final?
PostPosted: Mon Dec 20, 2010 9:01 am 
Newbie

Joined: Tue Jun 01, 2010 4:18 am
Posts: 11
Hi all,
I have a problem when use hibernate native query with Hibernate 3.6.0 final. For example, I have non ORM database table named application_log:

Code:
CREATE TABLE IF NOT EXISTS application_log (
  application_log_id bigint(20) NOT NULL AUTO_INCREMENT,
  log_code char(3) NOT NULL,
  message varchar(255) NOT NULL,
  PRIMARY KEY (application_log_id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

INSERT INTO `application_log` (`application_log_id`, `log_code`, `message`) VALUES(1, '100', 'Transaction OK.');
INSERT INTO `application_log` (`application_log_id`, `log_code`, `message`) VALUES(2, '100', 'Transaction OK.');
INSERT INTO `application_log` (`application_log_id`, `log_code`, `message`) VALUES(3, '200', 'Transaction canceled by user.');


Then I try to get all the data using:

Code:
sessionFactory.getCurrentSession().createNativeQuery("select * from application_log");


The data has been retrieve successfully, but the problem is in character (char) data type handling. As you may see, the log_code is defined in char(3), but when I call it UI / print it in the controller, the result is only show char[1] for each data. It's mean that, in the UI, it's just show:
Code:
LOG CODE | MESSAGE        |
1          Transaction OK
1          Transaction OK
2          Transaction OK


It's the first. Second problem, If I use DTO style:

Code:
session.createSQLQuery(sb.toString()).setResultTransformer(Transformers.aliasToBean(ApplicationLogDTO.class)).list();


for handling this, the exception has been raised:

Code:
java.sql.SQLException: Column 'application_log_id' not found.
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
   at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
   at com.mysql.jdbc.ResultSetImpl.getBigDecimal(ResultSetImpl.java:1414)
....


If someone has been interesting how the complete code works, cross checks whether my code wrong instead, and or when the strange and exception raises, you can checked this in github.

Thanks.


Top
 Profile  
 
 Post subject: Re: Bug in SQL Native Query with Hibernate 3.6.0 Final?
PostPosted: Wed Dec 22, 2010 1:19 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
I've had this problem, but only with SQL Server (HSQLDB seems to work fine). It must have something to do with Hibernate's ability to determine the types returned by a native query, and some databases not behaving themselves when delivering column metadata. You might have to manually define everything that's getting returned by your query:
Code:
session.createSQLQuery( "select application_log_id, log_code, message from application_log" )
  .addScalar( "application_log_id", StandardBasicTypes.LONG )
  .addScalar( "log_code", StandardBasicTypes.STRING )
  .addScalar( "message", StandardBasicTypes.STRING )
  .setResultTransformer( Transformers.aliasToBean( ApplicationLogDTO.class ) )
  .list();


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.