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.