-->
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: Customize the MySQL Dialect to map Boolean to TINYINT(1)
PostPosted: Tue Nov 09, 2010 9:06 am 
Newbie

Joined: Fri May 09, 2008 8:30 am
Posts: 10
Hello,

I would like to customize the MySQL dialect (e.g. the MySQL5InnoDBDialect) so that it uses a TINYINT(1) column type for a Boolean property, instead of BIT(1).

I looked at the class "org.hibernate.dialect.Dialect" and found

Code:
registerHibernateType( Types.BIT, Hibernate.BOOLEAN.getName() );


This is the only place where I could find something with "boolean".

Now I thought I might just add a

Code:
registerHibernateType( Types.TINYINT, Hibernate.BOOLEAN.getName() );


But "Types.TINYINT" is already "mapped" to "Hibernate.BYTE.getName()" some lines below, and that would overwrite the association for "Types.TINYINT".


Does anyone have a hint, how to alter the dialect class to use a TINYINT(1) column type for Boolean class properties?


Thanks a lot for your help!


Top
 Profile  
 
 Post subject: Re: Customize the MySQL Dialect to map Boolean to TINYINT(1)
PostPosted: Wed Nov 10, 2010 12:22 pm 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Do something like following:

Code:
public class MySQLCustomDialect extends org.hibernate.dialect.MySQL5InnoDBDialect {

    public MySQLCustomDialect() {
        super();
        registerColumnType(Types.BIT, "TINYINT(1)");
    }
   
}


Then use your new "MySQLCustomDialect" as your dialect.


Top
 Profile  
 
 Post subject: Re: Customize the MySQL Dialect to map Boolean to TINYINT(1)
PostPosted: Wed Nov 10, 2010 1:00 pm 
Newbie

Joined: Fri May 09, 2008 8:30 am
Posts: 10
Hello r,

thanks a lot for your answer. So, it is that easy :)

I think I had the wrong approach. So, BOOLEAN will be mapped to the "internal" Types.BIT, and with your modification, the "Types.BIT" will use the column type "TINYINT(1)", overwriting the "registerColumnType( Types.BIT, "bit" );" from "org.hibernate.dialect.MySQLDialect" (understood correctly?)

One last question please:

Is the custom Dialect with "TINYINT(1)" for a boolean value "compatible" with the standard "MySQL5InnoDBDialect" using "BIT" for a boolean value, so that I can save an object with "MySQLCustomDialect" and load it with "MySQL5InnoDBDialect" (assuming that there is some smart handling when Hibernate finds a "0" or "1" as the column value when translating it to a boolean property)?


Top
 Profile  
 
 Post subject: Re: Customize the MySQL Dialect to map Boolean to TINYINT(1)
PostPosted: Wed Nov 10, 2010 1:24 pm 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
I am not sure what you are going to do, but I think the answer is yes.


Top
 Profile  
 
 Post subject: Re: Customize the MySQL Dialect to map Boolean to TINYINT(1)
PostPosted: Wed Nov 10, 2010 5:59 pm 
Newbie

Joined: Fri May 09, 2008 8:30 am
Posts: 10
Quote:
I am not sure what you are going to do, but I think the answer is yes.


Well, I just want to do what I initially wrote - I want TINYINT(1) as the column type for boolean values (which you already solved :) ), because this is more common in the MySQL area instead of using BIT (for example, the famous HeidiSQL MySQL Frontend even does not display BIT values, and there is just no need to fix this bug, because the majority seems to use TINYINT(1)).

The question about the compatibility is just out of curiosity; maybe I use the custom dialect, and another one writes a test case and "forgets" to configure that custom dialect; will this lead to an error or not. But this is, of course, a minor issue which I can test for myself.


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.