-->
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.  [ 7 posts ] 
Author Message
 Post subject: mysql decimal/numeric datatype issue
PostPosted: Thu Apr 19, 2007 10:32 am 
Newbie

Joined: Thu Jul 21, 2005 9:51 am
Posts: 2
Location: London, UK
hi,

Hibernate version: 3.2.0
Name and version of the database: MySQL 5.0.27 InnoDB on windows

i'm doing schema validation with

<propertyname="dialect">
org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hbm2ddl.auto">validate</property>

against a database schema that was created in MySQL without the help of hibernate.

when i do schema validation against a table with a decimal/numeric column the hibernate schema validator always reports a datatype mismatch, saying that it expected "numeric".

my investigation resulted in these findings:

i observe that MySQL always reports numeric/decimal columns as decimal. i.e.,

CREATE TABLE `T` (
`d` decimal(10,0) NULL,
`n` numeric(10,0) NULL
)

will create a table where both columns d and n are of dataype decimal.

the hibernate MySQL5InnoDBDialect ultimately inherits from MySQLDialect and this registers the following datatype:

registerColumnType(2, "numeric($p,$s)");

indeed i find that if i change that to

registerColumnType(2, "decimal($p,$s)");

the aforementioned problems go away.

is this a genuine bug?

cheers,
gerald

_________________
Gerald Loeffler, Enterprise Java Architect
http://www.gerald-loeffler.net


Top
 Profile  
 
 Post subject: I had the same problem too
PostPosted: Tue Apr 24, 2007 9:43 am 
Newbie

Joined: Sun Sep 11, 2005 7:46 pm
Posts: 15
Hibernate version: 3.2.2
Name and version of the database: MySQL 5.0.28 InnoDB on Linux Debian 4.0


I'm doing schema validation with:
Code:
@Column(name = "CHO_Factor", scale=2, precision=4, nullable=true)
  public BigDecimal getChoFactor() {
    return choFactor;
  }


Got the result:

Code:
javax.persistence.PersistenceException: org.hibernate.HibernateException: Wrong column type: CHO_Factor, expected: numeric(4,2)


Gerald's fix looks pretty good, since MySql does use "decimal" internally for numeric type. My question is: how do I call registerColumnType(2,"decimal($p,$s)")?

And there lacks sql-type equivalent in the annotation....otherwise I could also supress this problem by writing

Code:
@Column(name = "CHO_Factor", sqltype="decimal(4,2)", nullable=true)
  public BigDecimal getChoFactor() {
    return choFactor;
  }


Thanks in advance!

Ellen


Top
 Profile  
 
 Post subject: Re: I had the same problem too
PostPosted: Tue Apr 24, 2007 11:19 am 
Newbie

Joined: Thu Jul 21, 2005 9:51 am
Posts: 2
Location: London, UK
hi ellen,

ellen wrote:
My question is: how do I call registerColumnType(2,"decimal($p,$s)")?


The only solution i saw was to simply create my own patched mysql dialects based on the mysql dialects that ship with hibernate:

- MySQLPatchedDialect is identical to MySQLDialect but in it's constructor calls registerColumnType(2, "decimal($p,$s)") (rather than registerColumnType(2, "numeric($p,$s)")).
- MySQL5PatchedDialect is identical to MySQL5Dialect but extends MySQLPatchedDialect (rather than MySQLDialect)
- MySQL5InnoDBPatchedDialect is identical to MySQL5InnoDBDialect but extends MySQL5PatchedDialect (rather than MySQL5Dialect)
- in my hibernate mapping file i then use MySQL5InnoDBPatchedDialect rather than MySQL5InnoDBDialect.

Not very elegant but safe ;-)

Please note that i've also created the JIRA issue HHH-2582 for this.

cheers,
gerald

_________________
Gerald Loeffler, Enterprise Java Architect
http://www.gerald-loeffler.net


Top
 Profile  
 
 Post subject: looked at HHH-2582
PostPosted: Tue Apr 24, 2007 12:54 pm 
Newbie

Joined: Sun Sep 11, 2005 7:46 pm
Posts: 15
Hi Gerald,

I looked at the HHH-2582, added a vote and added a comment. Thank you very much for this hint. :-) This type-mismatch problem has bugged me for the whole day, your solution made me productive again. :-)


Best Regards,
Ellen


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 27, 2007 7:20 pm 
Newbie

Joined: Sun Sep 11, 2005 7:46 pm
Posts: 15
I downloaded the 3.2.4 sp1 and saw that this problem is still not solved. It is causing a lot of problem for MySQL DB users and it is so easy to fix, could someone in the hibernate team please modify one line of code so that I do not have to patch again and again whenever there is a new release of hibernate?

In the package:
org.hibernate.dialect

In the file:
MySQLDialect.java

change line 39 from
registerColumnType( Types.NUMERIC, "numeric($p,$s)" );

to
registerColumnType( Types.NUMERIC, "decimal($p,$s)" );

And that's it.

I saw this file was last changed on 2006-12-08 by Mr. steve.ebersole@jboss.com. If Mr. Ebersole is around could you please have a look? thank you very much!


Ellen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 4:31 pm 
Newbie

Joined: Tue Jan 27, 2009 4:27 pm
Posts: 2
I had the same issue with DB2400Dialect.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 12:01 pm 
Newbie

Joined: Tue Jan 27, 2009 4:27 pm
Posts: 2
and
registerColumnType( Types.VARCHAR, "char($l)" );


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