cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Gentry <mgen...@masslight.net>
Subject Re: Reading boolean values stored in a database as chars
Date Wed, 07 Oct 2015 19:52:00 GMT
I'm generally 'lazier' than Mike.  Assuming there aren't too many of them,
I just map them in the subclass by hand.

For example, if there is a "foo" column, I'll map it in Cayenne as
getFooString()/setFooString() and then in the subclass:

public Boolean isFoo()
{
    if (StringUtils.equalsIgnoreCase(super.getFooString(), "f")
        return Boolean.FALSE;
    else if (StringUtils.equalsIgnoreCase(super.getFooString(), "t")
        return Boolean.TRUE;
    else
        return null;
}

public boolean setFoo(Boolean value)
{
    if (value == Boolean.FALSE)
        super.setFooString("f");
    else if (value == Boolean.TRUE)
        super.setFooString("t");
    else
        super.setFooString(null);
}

Of course, you'd probably want to refactor that into a helper method, but
you get the basic idea.

mrg

PS. Forgive any typos, please.


On Wed, Oct 7, 2015 at 3:10 PM, Mike Kienenberger <mkienenb@gmail.com>
wrote:

> Or sometimes it's easier to use a custom template and generate
> additional methods that automatically convert between String and
> boolean values.
>
> On Wed, Oct 7, 2015 at 3:08 PM, John Huss <johnthuss@gmail.com> wrote:
> > Check out the code for org.apache.cayenne.access.types.BooleanType and
> it's
> > subclasses.  I think you need to create your own extended type to handle
> > it.  Not sure if you can have multiple different BooleanTypes in the same
> > runtime, but you could have one that detects and switches
> > behavior dynamically I guess.
> >
> > On Wed, Oct 7, 2015 at 12:19 PM Hugi Thordarson <hugi@karlmenn.is>
> wrote:
> >
> >> Hi all.
> >>
> >> Oh the joys of working with legacy databases… :) I’m now using a
> database
> >> that sometimes stores boolean values as a char field with the value ’t’
> or
> >> ‘f’ (true or false). Can Cayenne help me map these fields to actual
> >> booleans in my entities so I don’t have to think about this in my logic?
> >>
> >> Cheers,
> >> - hugi
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message