You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Olivier Antoine <ol...@gmail.com> on 2006/01/19 23:20:13 UTC
boolean JavaType and mapping
Hello everyone,
I have to re-write a website with an existant MySQL database with an
important number of data ...so i could not change database structure
(column type and so on)
I m'a sking if it's possible and so ...How to?
make a mapping between
a boolean from my java bean class and an integer in my database column
associated?
for example when in my database I have 1 it return true in my boolean
and inverse.
thanks a lot,
Olivier
Re: boolean JavaType and mapping
Posted by "Albert L. Sapp" <as...@uiuc.edu>.
Olivier Antoine wrote:
> Hello everyone,
> I have to re-write a website with an existant MySQL database with an
> important number of data ...so i could not change database structure
> (column type and so on)
>
> I m'a sking if it's possible and so ...How to?
>
> make a mapping between
> a boolean from my java bean class and an integer in my database column
> associated?
>
> for example when in my database I have 1 it return true in my boolean
> and inverse.
>
> thanks a lot,
> Olivier
>
>
In our bean, we have following.
private String accessStatus;
private boolean active;
/*
* This pair of getters and setters obscures the fact that the
underlying datatype is
* a string type and not a boolean type. Refrain from using the
string version of these
* methods. Only ibatis will actually call them. We will always
call the boolean counterparts.
*/
public String getAccessStatus()
{
return accessStatus;
}
public void setAccessStatus(String accessStatus)
{
/* Make sure only true and false are even tried. Since this
data *should* only
* come from the db (i.e. we never call this), it should be fine.
* We throw a manager excpetion if bad values are put in and do
not change the
* the status of the variable.
*/
logger.finest("AccessStatus = " + accessStatus);
if(accessStatus==null)
{
this.active = false;
this.accessStatus = accessStatus;
}else if(accessStatus.equalsIgnoreCase("true"))
{
this.active = true;
this.accessStatus = accessStatus;
}
else if(accessStatus.equalsIgnoreCase("false"))
{
this.active = false;
this.accessStatus = accessStatus;
}
}
public boolean isActive() {
return active;
}
public void setActive(boolean isActive) {
this.active = isActive;
// Also, update the local accessStaus variable, so ibatis can
persist it to oracle.
if(isActive)
{
this.accessStatus = "TRUE";
}
else
{
this.accessStatus = "FALSE";
}
}
This works for us. I would assume you would just look for a integer or
whatever in your case.
HTH
Al