-->
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: How to map Java byte to smallint instead of tinyint
PostPosted: Fri Feb 10, 2012 6:21 pm 
Newbie

Joined: Fri Feb 10, 2012 5:18 pm
Posts: 2
We have encountered a problem in persisting Java byte type to SqlServer. Basically Java byte has a range from -128 to 127, whereas SqlServer tinyint has a range from 0 to 255. We intend to address the mismatch by mapping Java byte to smallint.

So far, we have managed to customize the type mapping, and verified that it indeed created a table with smallint column. At runtime, we were able to save non-negative value to the column. However if we have a negative value (say -1), we would hit exception ‘[JDBC SQLServer Driver]Value can not be converted to requested type.’

I took a quick look at org.hibernate.type.ByteType, and see it is mapped to JDBC tinyint. Does that explain what we experienced at runtime? How do we influence Hibernate to map it differently at runtime?


Top
 Profile  
 
 Post subject: Re: How to map Java byte to smallint instead of tinyint
PostPosted: Tue Feb 14, 2012 3:34 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi foggy,

I just have read

https://hibernate.onjira.com/browse/HHH-7060 and https://hibernate.onjira.com/browse/HHH-6815

today, and according what's written there, the problem is solved if you use Hibernate version 4:
if you use Hibernate version 4 then Java byte should be mapped to smallint.

Alternatively if you don't cant switch to hibernate4, you can also write and use a custom SQLServerDialect like following:

Code:
public class MyCustomSQLServerDialect extends SQLServer2008Dialect {
    @Override
    protected SqlTypeDescriptor getSqlTypeDescriptorOverride( int sqlCode ) {
        return sqlCode == Types.TINYINT ? SmallIntTypeDescriptor.INSTANCE : super.getSqlTypeDescriptorOverride(sqlCode);
    }
}


If you bootstrap the database with MyCustomSQLServerDialect then the byte-type properties should be mapped as smallint types.


Top
 Profile  
 
 Post subject: Re: How to map Java byte to smallint instead of tinyint
PostPosted: Thu Feb 16, 2012 9:41 pm 
Newbie

Joined: Fri Feb 10, 2012 5:18 pm
Posts: 2
Thanks, good to know that Hibernate is doing something on this issue.

We are still on Hibernate 3.3.2, which seems to predate SqlTypeDescriptor (3.6?). I will talk to folks to understand the impact of upgrading to 4.0 in the near future. One question about the proposed solution. Would that affect all Java types that are mapped to JDBC tinyint? It is conceivable that we would develop a UserType thatcan be losslessly represented by a tinyint, and it would inadvertently be treated as a smallint.


Top
 Profile  
 
 Post subject: Re: How to map Java byte to smallint instead of tinyint
PostPosted: Fri Feb 17, 2012 3:08 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Would that affect all Java types that are mapped to JDBC tinyint?


Yes, but concretely byte is the unique basic java type which is mapped to Standard SQL-build-in type TINYINT


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.