| Author |
Problem with Mapping between 1 bit column and boolean field
|
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Hi I am having a problem with entity beans in EJB 3.0. I am using jBoss 4.0.4 application server with MySQL 5.0.
I have a field in the database whose size is 1 bit. It is mapped to a boolean field in the class. Now I think that when the value in the database is 1, then the boolean field must be set to true. But this is not happening. The boolean field's value is evaluating to false. I don't what is the reason behind this. Can anyone help me with this :roll:
Thanks in advance
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8212
|
|
|
If you are using MySQL5, create the column in the database as a BOOLEAN type instead of BIT type. See if that works.
|
[My Blog] [JavaRanch Journal]
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Working like a charm now. Thanks again Jaikiran
But this is a very big project and it is working on the system of everyone on the team. I don't know whether they will let me change the column type or not :roll:
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8212
|
|
Ankit Garg wrote:
But this is a very big project and it is working on the system of everyone on the team. I don't know whether they will let me change the column type or not
You have one other option which you could try out. I'm not sure whether its going to work, though. But since you are using MySQL 5.x, you definitely should be using MySQL5Dialect with Hibernate. Have you configured your hibernate configuration file (or if you are using JPA, then persistence.xml) to use org.hibernate.dialect.MySQL5Dialect
|
 |
Jerwin Louise Uy
Ranch Hand
Joined: Oct 27, 2007
Posts: 75
|
|
@Ankit
There is another work around given that you do not have the authority to change the DDL.
Simply mapped the BIT column to the corresponding types in Java. Then create an accessor method that simply return boolean with respect to the instance variable that maps to the BIT column. You do not need to have getters/setters for the instance variable mapped to the BIT column in order not to confuse the callers of the class.
@Jaikiran
I do not think EJB 3.0 supports that kind of functionality. That is proprietary to Hibernate in itself. If you simply use the Hibernate as a persistence provider, then it will be constrained
to the rules and specs of EJB 3.0.
Regards.
|
Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Well this is my persistence.xml file
I think jaikiran you are talking about the provider value i.e. I need to write org.hibernate.dialect.MySQL5Dialect instead of org.hibernate.ejb.HibernatePersistence am I right?? Also Jerwin I can't change the class implementation either. They will not allow me to do that
|
 |
Jerwin Louise Uy
Ranch Hand
Joined: Oct 27, 2007
Posts: 75
|
|
If you do not have access to the entity beans, then it is a leakage of concerns, i.e. the concern of the domain model objects proliferated the concern of the client code.
Really, it is just a matter of "them"(the one in charge of the creating the domain model objects) testing the finder methods whether the mapping of the BIT column really reflected the expected Boolean counterpart.
I suggest that you inform the people in charge of the entity beans because if you can't change the domain model, then there is no way to get the right value unless you create another entity bean mapped to the same table.
Regards.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Jerwin this is really a matter of what THEY want. I can't help it. But since no one was able to solve my problem, so THEY allowed me to have a local copy of the database to work on. So now I don't have to worry about this anymore
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8212
|
|
Jerwin Louise Uy wrote:
@Jaikiran
I do not think EJB 3.0 supports that kind of functionality. That is proprietary to Hibernate in itself. If you simply use the Hibernate as a persistence provider, then it will be constrained
to the rules and specs of EJB 3.0.
Jerwin,
The persistence.xml is allowed to configure provider specific properties through the "properties" element. Here's an example:
However, i do agree that these properties are Hibernate specific and will be of no use when some other JPA implementation is used.
Jerwin Louise Uy wrote:
If you do not have access to the entity beans, then it is a leakage of concerns, i.e. the concern of the domain model objects proliferated the concern of the client code.
Really, it is just a matter of "them"(the one in charge of the creating the domain model objects) testing the finder methods whether the mapping of the BIT column really reflected the expected Boolean counterpart.
+1
|
 |
 |
|
|
subject: Problem with Mapping between 1 bit column and boolean field
|
|
|