-->
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.  [ 5 posts ] 
Author Message
 Post subject: Support for MySQL native sql Funtions in SELECT clause
PostPosted: Mon Feb 06, 2006 1:25 pm 
Newbie

Joined: Mon Feb 06, 2006 11:52 am
Posts: 3
Location: London, UK
Hibernate version: 3.1
MySQL Version: 5.0.18
MySQL Connector Version: 3.1.8

Hi,

I'm having some problems running some MySQL native sql queries through Hibernate.

The first query looks like this:

Code:

SELECT owner_id AS owner,
               sum(version) AS total
FROM record
WHERE start >= :fromDate AND start <= :toDate
GROUP BY owner_id


The query is executed using the following code:

Code:
                // open a new Hibernate session
      session = HibernateFactory.newSession();
            
      // setup the query
      Query extractQuery = session.createSQLQuery(query);
      
      // bind the parameter values
      bindParameters(extractQuery);
      
      // execute the query, store the results in an instance variable
      resultSet = extractQuery.scroll(ScrollMode.FORWARD_ONLY);


When executing this through Hibernate the following exception is generated:

Code:
Exception in thread "main" org.hibernate.MappingException: No Dialect mapping for JDBC type: 3
   at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
   at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
   at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:192)
   at org.hibernate.loader.custom.CustomLoader.getHibernateType(CustomLoader.java:170)
   at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:138)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1678)
   at org.hibernate.loader.Loader.scroll(Loader.java:2223)
   at org.hibernate.loader.custom.CustomLoader.scroll(CustomLoader.java:124)
   at org.hibernate.impl.SessionImpl.scrollCustomQuery(SessionImpl.java:1584)
   at org.hibernate.impl.AbstractSessionImpl.scroll(AbstractSessionImpl.java:126)
   at org.hibernate.impl.SQLQueryImpl.scroll(SQLQueryImpl.java:192)
   at stingray.extract.fetcher.NativeSQLFetcher.execute(NativeSQLFetcher.java:100)


I've identified that it is the SUM() in the SELECT clause that is causing the issue. I receive the same error when performing division in the SELECT clause. For example:

Code:
SELECT quoted / on_cover
FROM record


However using COUNT() in the SELECT clause does work. I assume that this means that not all of the MySQL functions and operators are supported in the Hibernate dialect for MySQL at the moment. Is this correct?

Other than resorting to using JDBC directly are there any ways of using the MySQL native functions described through Hibernate?

Thanks in advance,

Nick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 7:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
There are a set of core functions support across all dialects and dialect specific functions - see Dialect and MySQLDialect for the details. You do need to make sure your dialect is correct. In anycase, Sum is a core function - the error indicates its a type conversion issue rather than a function issue - what is the type being passed the the sum function? The division (and most other) operator is not supported by the parser at this point.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 6:24 am 
Newbie

Joined: Mon Feb 06, 2006 11:52 am
Posts: 3
Location: London, UK
Thanks for the prompt reply David.

The column being summed is defined as an Integer in MySQL, defined like this in the Hibernate config:

Code:
<version name="version" column="version" type="int"/>


The table is generated by Hibernate as:

Code:
CREATE TABLE `stingray`.`record` (
   `id` BIGINT NOT NULL,
   `class` VARCHAR(255) DEFAULT '' NOT NULL,
   `version` INT DEFAULT '' NOT NULL,
   `number` VARCHAR(255),
   `status` VARCHAR(255),
   `customerName` VARCHAR(255),
   `tradingName` VARCHAR(255),
   `business` TEXT,
   `created` DATETIME,
   `submit` DATETIME,
   `required` DATE,
   `due` DATE,
   `start` DATE,
   `end` DATE,
   `effectiveFrom` DATE,
   `effectiveTo` DATE,
   `duration` INT,
   `pol_ref` BIGINT,
   `locked` null,
   `flags` VARCHAR(255),
   `owner_id` BIGINT,
   `underwriter_id` BIGINT,
   `policy_id` BIGINT,
   PRIMARY KEY (`id`)
) ENGINE=MyISAM;


I get the same error if I perform a sum() on the policy_ref field, which is a BIGINT in the database and defined like this in the hibernate config file:

Code:
<property name="policyId" column="pol_ref" not-null="false" type="long"/>


So the hibernate table definition appears sensible, and summing these columns seems like a resonable thing to do and works as expected if I execute the query through MySQL directly.

I've tried wrapping the SELECT wiith functions with another select with no functions.

Code:
SELECT owner,
               total
FROM ( SELECT owner_id AS owner,
                              sum(version) AS total
               FROM record
               WHERE start >= :fromDate AND start <= :toDate
              GROUP BY owner_id ) AS results


But that hasnt worked because Hibernate is still unable to determine the underlying type of the total aliased column.

Is there any workaround to help Hibernate determine the datatype of a derived column, or will I need to resort to directly using JDBC?

Cheers,


Nick


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
My head was in HQL land. Your query does not define that a scalar is being returned so it does not know the type. In anycase, my first reaction is why are you not using HQL for this, eg,

HQL
Code:
SELECT r.owner_id, sum(r.version)
FROM record r
WHERE r.start >= :fromDate AND r.start <= :toDate
GROUP BY r.owner_id


In anycase, staying with the Native SQL approach change call to.

Code:
Query extractQuery = session.createSQLQuery(query);
extractQuery.addScalar("total", Hibernate.INTEGER);


There are other ways to define the mapping, see the reference manual for details.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 12:48 pm 
Newbie

Joined: Mon Feb 06, 2006 11:52 am
Posts: 3
Location: London, UK
Thanks David, explicitally defining the result set types worked a treat.


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