You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Markus Reich <ma...@markusreich.at> on 2013/04/22 10:51:12 UTC

Convert Oracle Type NUMBER to java.lang.Boolean

Hi,

I've a problem with using boolean values in my cayenne class.
In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
Now I thought I can map this to a Boolean?
I came to this because it also works when I select with an Expression:
ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));

But when I make a check with getDeleted()==false, it doesn't work as
getDeleted() always returns false?

What would be the best approach to transform a Oracle NUMBER to a Boolean?

Markus

Re: Convert Oracle Type NUMBER to java.lang.Boolean

Posted by Andrus Adamchik <an...@objectstyle.org>.
To the best of my knowledge thus should work on 3.1B2. 

On Apr 22, 2013, at 1:36 PM, Markus Reich <ma...@markusreich.at> wrote:

> ok, thx for reply, I'm using version 3.1 B2, I'll try the newest version
> 
> Meex
> 
> 
> 2013/4/22 Andrus Adamchik <an...@objectstyle.org>
> 
>> Actually this should just work. No hacks are needed. But it should be
>> mapped as NUMBER on the DbEntity side, and as java.lang.Boolean - on the
>> ObjEntity side. If it doesn't, it is a bug that we need to fix.
>> 
>> Andrus
>> 
>> On Apr 22, 2013, at 4:51 AM, Markus Reich <ma...@markusreich.at>
>> wrote:
>>> Hi,
>>> 
>>> I've a problem with using boolean values in my cayenne class.
>>> In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
>>> Now I thought I can map this to a Boolean?
>>> I came to this because it also works when I select with an Expression:
>>> ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));
>>> 
>>> But when I make a check with getDeleted()==false, it doesn't work as
>>> getDeleted() always returns false?
>>> 
>>> What would be the best approach to transform a Oracle NUMBER to a
>> Boolean?
>>> 
>>> Markus
>> 
>> 
> 
> 
> -- 
> *Markus Reich*
> Moosbach 28/2
> 6392 St. Jakob i.H.
> www.markusreich.at / www.meeximum.at
> markus.reich@markusreich.at


Re: Convert Oracle Type NUMBER to java.lang.Boolean

Posted by Markus Reich <ma...@markusreich.at>.
ok, thx for reply, I'm using version 3.1 B2, I'll try the newest version

Meex


2013/4/22 Andrus Adamchik <an...@objectstyle.org>

> Actually this should just work. No hacks are needed. But it should be
> mapped as NUMBER on the DbEntity side, and as java.lang.Boolean - on the
> ObjEntity side. If it doesn't, it is a bug that we need to fix.
>
> Andrus
>
> On Apr 22, 2013, at 4:51 AM, Markus Reich <ma...@markusreich.at>
> wrote:
> > Hi,
> >
> > I've a problem with using boolean values in my cayenne class.
> > In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
> > Now I thought I can map this to a Boolean?
> > I came to this because it also works when I select with an Expression:
> > ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));
> >
> > But when I make a check with getDeleted()==false, it doesn't work as
> > getDeleted() always returns false?
> >
> > What would be the best approach to transform a Oracle NUMBER to a
> Boolean?
> >
> > Markus
>
>


-- 
*Markus Reich*
Moosbach 28/2
6392 St. Jakob i.H.
www.markusreich.at / www.meeximum.at
markus.reich@markusreich.at

Re: Convert Oracle Type NUMBER to java.lang.Boolean

Posted by Aristedes Maniatis <ar...@maniatis.org>.
Yes, I do that all the time: integer in the database and boolean in the model. On the plus side when you (inevitably it seems) discover that your boolean actually needs 4 values, migrating to an enum is simple since you can leave the database alone and just change the model.

Remember the good old days when sex was just male/female/null?

Ari

On 23/04/13 1:23am, Andrus Adamchik wrote:
> Actually this should just work. No hacks are needed. But it should be mapped as NUMBER on the DbEntity side, and as java.lang.Boolean - on the ObjEntity side. If it doesn't, it is a bug that we need to fix.
>
> Andrus
>
> On Apr 22, 2013, at 4:51 AM, Markus Reich <ma...@markusreich.at> wrote:
>> Hi,
>>
>> I've a problem with using boolean values in my cayenne class.
>> In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
>> Now I thought I can map this to a Boolean?
>> I came to this because it also works when I select with an Expression:
>> ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));
>>
>> But when I make a check with getDeleted()==false, it doesn't work as
>> getDeleted() always returns false?
>>
>> What would be the best approach to transform a Oracle NUMBER to a Boolean?
>>
>> Markus
>

-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Convert Oracle Type NUMBER to java.lang.Boolean

Posted by Andrus Adamchik <an...@objectstyle.org>.
Actually this should just work. No hacks are needed. But it should be mapped as NUMBER on the DbEntity side, and as java.lang.Boolean - on the ObjEntity side. If it doesn't, it is a bug that we need to fix.

Andrus

On Apr 22, 2013, at 4:51 AM, Markus Reich <ma...@markusreich.at> wrote:
> Hi,
> 
> I've a problem with using boolean values in my cayenne class.
> In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
> Now I thought I can map this to a Boolean?
> I came to this because it also works when I select with an Expression:
> ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));
> 
> But when I make a check with getDeleted()==false, it doesn't work as
> getDeleted() always returns false?
> 
> What would be the best approach to transform a Oracle NUMBER to a Boolean?
> 
> Markus


Re: Convert Oracle Type NUMBER to java.lang.Boolean

Posted by Mike Kienenberger <mk...@gmail.com>.
I've always handled it by mapping the raw type, and creating Boolean
or boolean methods.   You just need to make sure they have unique
names (you can reset the database accessors to end in get*Raw/set*Raw
if need be).   Here's a contrived example.

    public boolean isRecurring()
    {
        boolean valueIfNull = false
        return booleanFromDatabaseType(this.getIsRecurringRaw(), valueIfNull);
    }

    public void setIsRecurringValue(boolean value)
    {
        this.setIsRecurringRaw(this.databaseTypeForBoolean(value));
    }

You write the valueFromDatabaseType methods once, and it becomes
trivial to use these for the rest.   If you want to get fancy, you can
have your cgen templates automatically do this conversion for you,
although you need to specify what happens with a null value in each
case if you want to use boolean instead of Boolean.

On Mon, Apr 22, 2013 at 8:40 AM, Michael Gentry <mg...@masslight.net> wrote:
> Hi Markus,
>
> I've always found booleans to be a little odd to work with, especially when
> you have allows null.  You could always define your own TRUE/FALSE enum (or
> ExtendedEnumeration) as 1 and 0 respectively and then do:
>
> ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY,
> MyEnum.FALSE));
>
> Keep in mind if your deleted property allows nulls, though, then false !=
> null.
>
> mrg
>
>
>
> On Mon, Apr 22, 2013 at 4:51 AM, Markus Reich
> <ma...@markusreich.at>wrote:
>
>> Hi,
>>
>> I've a problem with using boolean values in my cayenne class.
>> In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
>> Now I thought I can map this to a Boolean?
>> I came to this because it also works when I select with an Expression:
>> ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));
>>
>> But when I make a check with getDeleted()==false, it doesn't work as
>> getDeleted() always returns false?
>>
>> What would be the best approach to transform a Oracle NUMBER to a Boolean?
>>
>> Markus
>>

Re: Convert Oracle Type NUMBER to java.lang.Boolean

Posted by Michael Gentry <mg...@masslight.net>.
Hi Markus,

I've always found booleans to be a little odd to work with, especially when
you have allows null.  You could always define your own TRUE/FALSE enum (or
ExtendedEnumeration) as 1 and 0 respectively and then do:

ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY,
MyEnum.FALSE));

Keep in mind if your deleted property allows nulls, though, then false !=
null.

mrg



On Mon, Apr 22, 2013 at 4:51 AM, Markus Reich
<ma...@markusreich.at>wrote:

> Hi,
>
> I've a problem with using boolean values in my cayenne class.
> In Oracle DB the field has type NUMBER and contains only 0 or 1 as value.
> Now I thought I can map this to a Boolean?
> I came to this because it also works when I select with an Expression:
> ExpressionFactory.matchExp(MiiPickingcontainer.DELETED_PROPERTY, false));
>
> But when I make a check with getDeleted()==false, it doesn't work as
> getDeleted() always returns false?
>
> What would be the best approach to transform a Oracle NUMBER to a Boolean?
>
> Markus
>