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!
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.
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 ]
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.
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
posted 11 years ago
It was so simple, that hibernate has in-built substitution properties after going through the following post:
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 ]
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.
posted 11 years ago
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.
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 ]
posted 11 years ago
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.
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.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
posted 11 years ago
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 .