You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Daniel Perry <d....@netcase.co.uk> on 2005/09/06 17:49:40 UTC

Boolean2IntFieldConversion with TINYINT

A warning to anyone else who might try this:

Just been struggling with IllegalArgumentExceptions on boolean fields.
Anyway, it turns out that you cant have a boolean with
Boolean2IntFieldConversion mapped as a TINYINT - it MUST be INTEGER!!!

Daniel.


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Dudziak <to...@gmail.com>.
On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:
> True.
> 
> Although surely if you're passing an item through the conversion, it
> requires the conversion every time, so the default behaviour *should* never
> happen.  So, checking the extra types should only impact cases where you've
> not used INTEGER.  So, no penalty (except a few more bytes in class file) to
> people using it properly!

You'd still have the instanceof check, even if the check for Integer
is the first one.
There may however be another option. The field conversion obejct is
the same for every time the field is accessed, thus it can store
state. Upon first access in the Sql->Java direction the type returned
by the database could be determined and stored for later usage, thus
saving the cost in subsequent calls.
Could you open an issue in OJB's JIRA
(http://issues.apache.org/jira/browse/OJB) for this ?

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: Boolean2IntFieldConversion with TINYINT

Posted by Daniel Perry <d....@netcase.co.uk>.
True.

Although surely if you're passing an item through the conversion, it
requires the conversion every time, so the default behaviour *should* never
happen.  So, checking the extra types should only impact cases where you've
not used INTEGER.  So, no penalty (except a few more bytes in class file) to
people using it properly!

Daniel.

> -----Original Message-----
> From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> Sent: 07 September 2005 15:36
> To: OJB Users List
> Subject: Re: Boolean2IntFieldConversion with TINYINT
>
>
> On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:
>
> > Hmm... What about modifying Boolean2IntFeild itself so it handles both:
>
> <snip>
>
> Yes, this would be possible but the instanceof's have a possibly
> significant runtime cost, esp. since you would want to also handle
> Short, Long and possibly Character as well.
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Dudziak <to...@gmail.com>.
On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:

> Hmm... What about modifying Boolean2IntFeild itself so it handles both:

<snip>

Yes, this would be possible but the instanceof's have a possibly
significant runtime cost, esp. since you would want to also handle
Short, Long and possibly Character as well.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Franke <fr...@softenergy.de>.
Thomas Dudziak wrote:

> Therefore you'll have to either use BIT as the JDBC column type or
> write your own Boolean2Byte conversion (simply copy the code of the
> Boolean2Int conversion and replace Integer with Byte).
That's true. We have also our own conversions for example a Long2Calender
conversion.

Regards,

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: Boolean2IntFieldConversion with TINYINT

Posted by Daniel Perry <d....@netcase.co.uk>.
Hmm... What about modifying Boolean2IntFeild itself so it handles both:

    private static Byte B_TRUE = new Byte(1);

    public Object sqlToJava(Object source)
    {
        if (source instanceof Integer)
        {
            if (source.equals(I_TRUE))
            {
                return Boolean.TRUE;
            }
            else
            {
                return Boolean.FALSE;
            }
        }
        else if (source instanceof Byte)
        {
            if (source.equals(B_TRUE))
            {
                return Boolean.TRUE;
            }
            else
            {
                return Boolean.FALSE;
            }
        }
        else
        {
            return source;
        }
    }


Would the fact that it passes an Intger, not a Byte to jdbc be a problem ? i
dont think it will in most cases - eg. mysql i use tinyint when field type
is integer if i know it will only be small.  Anyway, i dont think this would
do any harm, and would save anyone else going through the pain of working
out what is going on!

Daniel.

> -----Original Message-----
> From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> Sent: 07 September 2005 09:45
> To: OJB Users List
> Subject: Re: Boolean2IntFieldConversion with TINYINT
>
>
> On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:
>
> > The following works fine:
> >
> > /**
> >  * @ojb.field jdbc-type="INTEGER"
> >  *
> >
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2
> IntFieldCo
> > nversion"
> >  */
> > protected boolean deleted;
> >
> > But, if you use:
> >
> > @ojb.field jdbc-type="TINYINT"
> >
> > Then, xdoclet does as it should:
> >
> >     <field-descriptor
> >         name="deleted"
> >         column="deleted"
> >         jdbc-type="TINYINT"
> >
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2
> IntFieldCo
> > nversion"
> >     >
> >
> > And ojb dies (although i am kinda sympathetic as it is using
> Boolean2Int,
> > not Boolean2Tinyint!):
> >
> > [PersistentField] ERROR: while set field:
> > [try to set 'object value' in 'target object'
> > target obj class: com.netcase.pol.bo.User
> > target field name: deleted
> > target field type: boolean
> > target field declared in: com.netcase.pol.bo.BaseBO
> > object value class: java.lang.Byte
> > object value: 0
> > ]
> >
> > ...
> >
> > Caused by: java.lang.IllegalArgumentException
> >         at
> >
> sun.reflect.UnsafeBooleanFieldAccessorImpl.set(UnsafeBooleanFieldA
> ccessorImp
> > l.java:68)
> >         at java.lang.reflect.Field.set(Field.java:656)
> >         at
> >
> org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAc
> cessImplNe
> > w.setValueFor(PersistentFieldDirectAccessImplNew.java:213)
>
>
> Ah, ok, now it is clear. This has nothing to do with XDoclet at all,
> rather the conversion that you specified, is wrong because it converts
> between boolean/java.lang.Boolean and int/java.lang.Integer. The
> problem arises because the JDBC driver returns a java.lang.Byte object
> for the TINYINT column and not an java.lang.Integer object that the
> conversion expects.
> Therefore you'll have to either use BIT as the JDBC column type or
> write your own Boolean2Byte conversion (simply copy the code of the
> Boolean2Int conversion and replace Integer with Byte).
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Dudziak <to...@gmail.com>.
On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:

> The following works fine:
> 
> /**
>  * @ojb.field jdbc-type="INTEGER"
>  *
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldCo
> nversion"
>  */
> protected boolean deleted;
> 
> But, if you use:
> 
> @ojb.field jdbc-type="TINYINT"
> 
> Then, xdoclet does as it should:
> 
>     <field-descriptor
>         name="deleted"
>         column="deleted"
>         jdbc-type="TINYINT"
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldCo
> nversion"
>     >
> 
> And ojb dies (although i am kinda sympathetic as it is using Boolean2Int,
> not Boolean2Tinyint!):
> 
> [PersistentField] ERROR: while set field:
> [try to set 'object value' in 'target object'
> target obj class: com.netcase.pol.bo.User
> target field name: deleted
> target field type: boolean
> target field declared in: com.netcase.pol.bo.BaseBO
> object value class: java.lang.Byte
> object value: 0
> ]
> 
> ...
> 
> Caused by: java.lang.IllegalArgumentException
>         at
> sun.reflect.UnsafeBooleanFieldAccessorImpl.set(UnsafeBooleanFieldAccessorImp
> l.java:68)
>         at java.lang.reflect.Field.set(Field.java:656)
>         at
> org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImplNe
> w.setValueFor(PersistentFieldDirectAccessImplNew.java:213)


Ah, ok, now it is clear. This has nothing to do with XDoclet at all,
rather the conversion that you specified, is wrong because it converts
between boolean/java.lang.Boolean and int/java.lang.Integer. The
problem arises because the JDBC driver returns a java.lang.Byte object
for the TINYINT column and not an java.lang.Integer object that the
conversion expects.
Therefore you'll have to either use BIT as the JDBC column type or
write your own Boolean2Byte conversion (simply copy the code of the
Boolean2Int conversion and replace Integer with Byte).

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: Boolean2IntFieldConversion with TINYINT

Posted by Daniel Perry <d....@netcase.co.uk>.
The following works fine:

/**
 * @ojb.field jdbc-type="INTEGER"
 *
conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldCo
nversion"
 */
protected boolean deleted;

But, if you use:

@ojb.field jdbc-type="TINYINT"

Then, xdoclet does as it should:

    <field-descriptor
        name="deleted"
        column="deleted"
        jdbc-type="TINYINT"
conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldCo
nversion"
    >

And ojb dies (although i am kinda sympathetic as it is using Boolean2Int,
not Boolean2Tinyint!):

[PersistentField] ERROR: while set field:
[try to set 'object value' in 'target object'
target obj class: com.netcase.pol.bo.User
target field name: deleted
target field type: boolean
target field declared in: com.netcase.pol.bo.BaseBO
object value class: java.lang.Byte
object value: 0
]

...

Caused by: java.lang.IllegalArgumentException
	at
sun.reflect.UnsafeBooleanFieldAccessorImpl.set(UnsafeBooleanFieldAccessorImp
l.java:68)
	at java.lang.reflect.Field.set(Field.java:656)
	at
org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImplNe
w.setValueFor(PersistentFieldDirectAccessImplNew.java:213)


Daniel.


> -----Original Message-----
> From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> Sent: 07 September 2005 09:18
> To: OJB Users List
> Subject: Re: [junk] Re: Boolean2IntFieldConversion with TINYINT
>
>
> On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:
> > What i mean, is that you cant do:
> >
> > <field-descriptor
> > name="deleted"
> > column="deleted"
> > jdbc-type="TINYINT"
> >
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2
> IntFieldCo
> > nversion"/>
> >
> > It would make sense to use tinyint for booleans.... as they
> only really need
> > 1bit!
> >
> > Problem is... i am using xdoclet to make the repository and
> torque to make
> > the database.  So, i have to use INTEGER in the xdoclet
> comment, so it ends
> > up as integer in the database.  I previously have put it as INTEGER then
> > used tinyint in mysql - but with xdoclet, everything is automated.
>
> What is the problem with this declaration ? Does XDoclet complain (it
> shouldn't) or does the JDBC driver complain or Torque ? Could you post
> the error and the generated SQL ?
>
> Btw, you could also try to use BIT as the datatype, it's meant to be
> used for boolean columns.
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: [junk] Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Dudziak <to...@gmail.com>.
On 9/7/05, Daniel Perry <d....@netcase.co.uk> wrote:
> What i mean, is that you cant do:
> 
> <field-descriptor
> name="deleted"
> column="deleted"
> jdbc-type="TINYINT"
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldCo
> nversion"/>
> 
> It would make sense to use tinyint for booleans.... as they only really need
> 1bit!
> 
> Problem is... i am using xdoclet to make the repository and torque to make
> the database.  So, i have to use INTEGER in the xdoclet comment, so it ends
> up as integer in the database.  I previously have put it as INTEGER then
> used tinyint in mysql - but with xdoclet, everything is automated.

What is the problem with this declaration ? Does XDoclet complain (it
shouldn't) or does the JDBC driver complain or Torque ? Could you post
the error and the generated SQL ?

Btw, you could also try to use BIT as the datatype, it's meant to be
used for boolean columns.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: [junk] Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Franke <fr...@softenergy.de>.
Daniel Perry wrote:

> What i mean, is that you cant do:
I see.

> Problem is... i am using xdoclet to make the repository and torque to make
> the database.  So, i have to use INTEGER in the xdoclet comment, so it ends
> up as integer in the database.  I previously have put it as INTEGER then
> used tinyint in mysql - but with xdoclet, everything is automated.
Then it is more an xdoclet problem than an ojb problem. :)

Regards,

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: [junk] Re: Boolean2IntFieldConversion with TINYINT

Posted by Daniel Perry <d....@netcase.co.uk>.
What i mean, is that you cant do:

<field-descriptor
name="deleted"
column="deleted"
jdbc-type="TINYINT"
conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldCo
nversion"/>

It would make sense to use tinyint for booleans.... as they only really need
1bit!

Problem is... i am using xdoclet to make the repository and torque to make
the database.  So, i have to use INTEGER in the xdoclet comment, so it ends
up as integer in the database.  I previously have put it as INTEGER then
used tinyint in mysql - but with xdoclet, everything is automated.

Daniel.

> -----Original Message-----
> From: Thomas Franke [mailto:franke@softenergy.de]
> Sent: 06 September 2005 17:19
> To: OJB Users List
> Subject: [junk] Re: Boolean2IntFieldConversion with TINYINT
>
>
> Daniel Perry wrote:
>
> > Boolean2IntFieldConversion mapped as a TINYINT - it MUST be INTEGER!!!
> I don't know what you exactly mean but we use this and it works out fine.
>
> <field-descriptor
> 	name="deleted"
> 	column="deleted"
> 	jdbc-type="INTEGER"
>
> conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2
> IntFieldConversion"
> />
>
> In the database the data type is tinyint.
>
> Regards,
>
> Thomas
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Franke <fr...@softenergy.de>.
Daniel Perry wrote:

> Boolean2IntFieldConversion mapped as a TINYINT - it MUST be INTEGER!!!
I don't know what you exactly mean but we use this and it works out fine.

<field-descriptor
	name="deleted"
	column="deleted"
	jdbc-type="INTEGER"
	conversion="org.apache.ojb.broker.accesslayer.conversions.Boolean2IntFieldConversion"
/>

In the database the data type is tinyint.

Regards,

Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Boolean2IntFieldConversion with TINYINT

Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, Daniel Perry <d....@netcase.co.uk> wrote:
> A warning to anyone else who might try this:
> 
> Just been struggling with IllegalArgumentExceptions on boolean fields.
> Anyway, it turns out that you cant have a boolean with
> Boolean2IntFieldConversion mapped as a TINYINT - it MUST be INTEGER!!!

This depends on the JDBC driver. Some can use Integer objects with
TINYINT columns, others can't (and require eg. Short or similar).

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org