-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate SUM() Issue
PostPosted: Fri Feb 18, 2005 2:08 am 
Newbie

Joined: Fri Feb 18, 2005 1:42 am
Posts: 2
Hi all. I've checked the documentation, the FAQs, searched the forums, and googled it, but I can't seem to find a solution to the problem I'm facing, so I would really appreciate any help some of you with a bit more experience with Hibernate could give me.

I am performing a very simple query where I sum() a byte (tinyint) field. However, obviously the result could easily be out of range, which is exactly the problem I'm having. It appears that the result is returned as the type of the field queried. Is this the intended functionality? Am I missing something?

Thanks!

Hibernate version:
2.1

Mapping documents:
<property name="level" column="level" type="byte" not-null="true"/>

Full stack trace of any exception that occurs:
java.sql.SQLException: Value '229' is out of range [-127,127]
at com.mysql.jdbc.ResultSet.getByte(ResultSet.java:681)
at com.mysql.jdbc.ResultSet.getByte(ResultSet.java:713)
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getByte(DelegatingResultSet.java:230)
at net.sf.hibernate.type.ByteType.get(ByteType.java:21)
at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
at net.sf.hibernate.hql.QueryTranslator.getResultColumnOrRow(QueryTranslator.java:1000)
at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:238)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:281)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1553)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1530)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1526)
...

Name and version of the database you are using:
MySQL 3.23.54

The generated SQL (show_sql=true):
select sum(userhero0_.level) as x0_0_ from user_her
oes userhero0_ where (userhero0_.user_id=? )

Debug level Hibernate log excerpt:

2005-02-17 23:35:45,910 [http-8080-Processor24] DEBUG - HQL: select sum(uh.level) from com.octopi.dal.UserHero a
s uh where uh.user.id = ?
2005-02-17 23:35:45,910 [http-8080-Processor24] DEBUG - SQL: select sum(userhero0_.level) as x0_0_ from user_her
oes userhero0_ where (userhero0_.user_id=? )
2005-02-17 23:35:45,910 [http-8080-Processor24] DEBUG - about to open: 0 open PreparedStatements, 0 open ResultS
ets
2005-02-17 23:35:45,910 [http-8080-Processor24] DEBUG - select sum(userhero0_.level) as x0_0_ from user_heroes u
serhero0_ where (userhero0_.user_id=? )
2005-02-17 23:35:45,911 [http-8080-Processor24] DEBUG - preparing statement
2005-02-17 23:35:45,911 [http-8080-Processor24] DEBUG - binding '1' to parameter: 1
2005-02-17 23:35:45,912 [http-8080-Processor24] DEBUG - processing result set
2005-02-17 23:35:45,912 [http-8080-Processor24] DEBUG - result row:
2005-02-17 23:35:45,912 [http-8080-Processor24] DEBUG - SQL Exception


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 10:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The sum() function passes the same type as a result of its operation to the result processor. Yes you can go over the size limits of the type.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 6:00 am 
Newbie

Joined: Fri Feb 18, 2005 1:42 am
Posts: 2
Wouldn't you say then, that it makes sum() a fairly unsafe function to use? It seems as if I'll need to change my underlying type to an integer just to avoid the exception, when I know it will never hold a value above 12. I mean, the way it works now, I can't imagine ever feeling reasonable assured it would work...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 6:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It would not matter what type you use the potential is there to overflow the type's storage size. There are alternatives but to me increasing the database' storage size is the best option. In many cases, its more efficient if you use main storage type for the underlying hardware architecture, eg, Integer could be more effcient though its hard to measure.


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