-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to create a native sql statement DAO method
PostPosted: Fri Nov 23, 2012 5:43 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello,

I'm trying to run a native sql statement in Hibernate and retrieve the results into a collection of domain objects, but I get the following exception:
Quote:
org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred while calling setter of com.thalasoft.learnintouch.core.domain.NavbarLanguage.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.thalasoft.learnintouch.core.domain.NavbarLanguage.id


Here is the DAO method:
Code:
   public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
      String statement = "select id, version, language_code as languageCode from navbar_language where navbar_id = :navbarId order by language_code nulls first";
      Query query = getSession().createSQLQuery(statement)
         .addScalar("id")
         .addScalar("version")
         .addScalar("languageCode");
      query.setLong("navbarId", navbar.getId());
      return query.setResultTransformer(Transformers.aliasToBean(NavbarLanguage.class)).list();
   }


I'm not sure about the use of the addScalar method. I tried it because otherwise the column names are returned uppercase.

I wonder if this is the way to go to create a native sql statement DAO method.


Top
 Profile  
 
 Post subject: Re: How to create a native sql statement DAO method
PostPosted: Sat Nov 24, 2012 3:32 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The Hibernate mapping is as follows:
Code:
    <class name="com.thalasoft.learnintouch.core.domain.NavbarLanguage" table="navbar_language" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native"><param name="sequence">sq_id_navbar_language</param></generator>
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <property name="languageCode" type="string">
            <column name="language_code" length="2" />
        </property>
        <many-to-one name="navbar" class="com.thalasoft.learnintouch.core.domain.Navbar" cascade="all">
            <column name="navbar_id" not-null="true" />
        </many-to-one>
    </class>

You can note that the id property is a java.lang.Integer type, and it builds and works fine against the MySql schema when building against MySql with the command: mvn clean install -Pmysql-test -Dtest=NavbarLanguageDaoTest

Note that the DAO method used against MySql is not the one displayed above but this one:
Code:
   public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
      if (navbarLanguageCustomDao != null) {
         return navbarLanguageCustomDao.findWithNavbar(navbar);
      } else {
         Criteria criteria = getSession().createCriteria(getPersistentClass());
         criteria.add(Restrictions.eq("navbar", navbar)).addOrder(Order.asc("languageCode"));
         return criteria.list();
      }
   }

Indeed, when running against MySql, there is no need to have a native sql DAO method, as the null values are ordered first by default on MySql, contrary to Oracle, which explains the native sql statement DAO method being used when building against Oracle.

So, only when I build against Oracle with the command: mvn clean install -Poracle-test -Dtest=NavbarLanguageDaoTest do I get the above exception.

Here are the schema tables definitions on both database servers.

For MySql:
Code:
CREATE TABLE `navbar_language` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `version` int(10) unsigned NOT NULL,
  `language_code` varchar(2) default NULL,
  `navbar_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `navbar_id` (`navbar_id`),
  KEY `navbar_id_2` (`navbar_id`,`language_code`),
  CONSTRAINT `navbar_language_ibfk_1` FOREIGN KEY (`navbar_id`) REFERENCES `navbar` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1


For Oracle:
Code:
create table navbar_language (
  id number(10) not null,
  version number(10) not null,
  language_code varchar2(2),
  navbar_id number(10) not null,
  constraint navbar_language_pk primary key (id),
  constraint navbar_language_fk1 foreign key (navbar_id) references navbar (id)
);
create sequence sq_id_navbar_language increment by 1 start with 1 nomaxvalue nocycle cache 10;
create or replace trigger tr_id_inc_navbar_language
before insert
on navbar_language
for each row
declare
begin
  if (:new.id is null)
  then
    select sq_id_navbar_language.nextval into :new.id from dual;
  end if;
end;


Top
 Profile  
 
 Post subject: Re: How to create a native sql statement DAO method
PostPosted: Wed Nov 28, 2012 7:29 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The custom DAO method was faulty. I had to use some explicit types in the addScalar methods.
Here is how the method now looks like:
Code:
   public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
      String statement = "select id, version, language_code as languageCode from navbar_language where navbar_id = :navbarId order by language_code nulls first";
      Query query = getSession().createSQLQuery(statement)
         .addScalar("id", StandardBasicTypes.INTEGER)
         .addScalar("version", StandardBasicTypes.INTEGER)
         .addScalar("languageCode");
      query.setInteger("navbarId", navbar.getId());
      return query.setResultTransformer(Transformers.aliasToBean(NavbarLanguage.class)).list();
   }


As a side note, I also changed the type from setLong to setInteger on the navbarId property although it was not the issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.