Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping a oracle boolean to Java boolean data type in hibernate

 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our oracle database (does not have boolean data type) uses a number, rather an integer with 0 and 1 to represent false and true java boolean equivalents. Is there a way to mention in the mapping file or using annotation (I have no idea about this!) to let hibernate take care of the retrieval and insertion of boolean fields or records directly.

One rather dirty problem we have in our code is inclusion of many if loops that check if the dynamically selected fields are of the type boolean when displaying or inserting records to display true or false while displaying and converting to 1 and 0 respectively!

Any suggestion in this regards would be appreciated!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should actually be pretty seamless.

In the hibernate.cfg.xml file, you will specify a dialect class for Hibernate to use. Hibernate will know how to map your Boolean values in you Java class to a binary, 1 or 0 value, in your database. Similary, Strings are converted to varchar and Char[] arrays are converting into clobs.

Are you running into any specific problem?

-Cameron McKenzie
[ September 10, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply! but could you explain briefly or point me to a web page to understand how and where in Hibernate.cfg.xml should we modify in order to enable this. What exactly is this procedure called?

As of now we have custom code to take care of this and I have too many issues because of this. We do use Oracle dialect but I don't know how we can map an integer (0 or 1) in oracle database column to a boolean field in Java.
[ May 01, 2008: Message edited by: Rama Krishna ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're really looking at the most fundamental setup steps.

Here's a tutorial that talks about how to set up Hibernate. It uses MySQL. I suggest you try and do something like this on your own. Spend a day getting a basic environment up, and doing a few queries and updates, and you'll quickly start to understand how Hibernate works.

How to set up Hibernate with MySQL and Verify the Installation

This tutorial has a hibernate.cfg.xml file at the end of it. You can see the dialect property being set for MySQL. Hibernate comes with different dialect classes for each of the major databases.

Good luck. You'll find that Hibernate solves many of these common problems seamlessly and elegantly.



-Cameron McKenzie
[ September 10, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really sorry I confused you.

1. I started off asking: how we can map a oracle boolean (in our case we store it as Number(1) ) to a java boolean data type.

2. You suggested making modifications in hibernate.cfg.xml

3. I then said that I would be interested to know the changes in hibernate.cfg.xml to get my problem working.

4. I got you confused!

I forgot to tell you the fact that I know how to create the cfg.xml, mapping files including mentioning the dialects and other properties.

What I don't know even now is a way around getting the oracle Number(1) column mapped to a java boolean field so that both select and update fields can be made by me by treating them as java fields in my Data Access Objects and hibernate will take care of the transformation.

Sorry again for the confusion
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woow,

It was so simple, that hibernate has in-built substitution properties after going through the following post:

http://www.coderanch.com/t/215316/ORM/java/Mapping-ENUM-or-CHAR-Boolean

All I had to do was:

change from

to


Now, the requirements require us to show Yes and No instead of true and false for the oracle field! Can these substitutions help us, if so how and where can I get started?
[ May 01, 2008: Message edited by: Rama Krishna ]
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now, the requirements require us to show Yes and No instead of true and false for the oracle field! Can these substitutions help us, if so how and where can I get started?



In addition to "boolean", you can specify "yes_no" and "true_false" mapping types.
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am missing something here.



If I say "yes_no" in the mapping file, I get an exception while accessing/setting the particular property



I believe it is because I have defined it as a String in my object file.



Anyways, I am asking the same question again: how can we map an oracle Number field (we use it as a boolean and in our case we store it as Number(1) field i.e., 1 and 0) to a java String with "yes" or "no" as a String for 1 and 0 in oracle field respectively.

-Cheers
Rama
[ May 04, 2008: Message edited by: Rama Krishna ]
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the question is not the same anymore: a "Java boolean" is, to me at least, a Boolean and not a String.
In your case, you may have to create your own mapping type.
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edvins,



When I said, "let me ask the same question again". I was actually referring to my last two posts only and not the post (or the post subject) I started off with.

I wonder how we can create custom mapping files in this case. I will try and look up the Hibernate reference again.

-Cheers
Rama
[ May 05, 2008: Message edited by: Rama Krishna ]
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found two links which pretty much explain what I am possibly looking for.

http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-types-custom

and

http://www.hibernate.org/261.html

But now I have two questions to ask.

1) By creating this custom type, can we by changing the dialect and pointing to another database, say SQL server, to a tinyint or another integer type also representing 0 and 1 for boolean use the same mapping file, object file and the application without any problem? We are designing our application to be able to be compatible with different databases at the same time.

2) What does the following intercept mean:

" Even though Hibernate's rich range of built-in types and support for components means you will very rarely need to use a custom type, it is nevertheless considered good form to use custom types for (non-entity) classes that occur frequently in your application. For example, a MonetaryAmount class is a good candidate for a CompositeUserType, even though it could easily be mapped as a component. One motivation for this is abstraction. With a custom type, your mapping documents would be future-proofed against possible changes in your way of representing monetary values. "
[ May 05, 2008: Message edited by: Rama Krishna ]
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before going into custom mapping types, I would consider using what is available in Hibernate (a Boolean property can be mapped easily to a database field), but if this fails, then there are custom mapping types as in the links that you found, or in the book. Haven't used this feature, so can't comment further.
 
Didier Bretin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to port a postgresql database to oracle and I have the problem of boolean field. Under oracle I have char(1) field. I tried to use the "true_false" type I setup in my .hbm.xml files.

When I try to execute my application the sql generated contained my_field = true and not my_field='t'.

Did I forgot to change something ?

Regards
 
Tim Holloway
Saloon Keeper
Posts: 18365
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with using booleans on databases which don't have native boolean data types is that there are so many different ways that people manage booleans in databases. Not only as Strings but as ints and occasionally other things as well. Most commonly, a string value of "T" or "F" is used or "1" and "0", but some people do "true"/"false", "yes"/"no" or even ".TRUE."/".FALSE." (FORTRAN-style).

I've been fortunate with Hibernate - the databases I worked with had native boolean types. But I've used JDO with Oracle, and it has the same problem. For that particular instance, the simple way was to specify each boolean field's values using an enumeration value pair. The JDO vendor also had the ability to globally specify custom mapping classes.
 
Didier Bretin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I updated from version 3.0.5 to version 3.2.6 and it works. It's a good news. Now I have a problem with timestamp .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic