You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Michael McCutcheon <mi...@frontiernet.net> on 2005/11/30 06:28:42 UTC
Derby with beans and null primitives
I'm implementing a utility class that populates 'beans' with data from
tables in derby. Suppose I have the following 'bean':
public class Person
{
private String firstName;
private String lastName;
private int weight;
public void setWeight(int inWeight)
{
this.weight = inWeight;
}
public int getWeight()
{
return this.weight;
}
}
I select out of a Person table to get the information to populate this bean.
But what happens if weight is nullable in the table?
Since you can't put nulls into primitives in Java, how is this handled?
This is a real problem because 0 is definately different than no value.
Also is there some standard for how JSP's deal with beans with primitive
types?
How should null primitives be handled in beans like this?
Thanks
Mike
Re: Derby with beans and null primitives
Posted by Thomas Dudziak <to...@gmail.com>.
On 11/30/05, Michael McCutcheon <mi...@frontiernet.net> wrote:
> Yes, I have heard of these, but there is a HUGE learning curve to
> these. What I need to do is fairly simple and does not require a
> complete 'persistence solution'. I just need to populate beans from
> resultsets and need to know how to handle null database integers with
> java primitives.
>
> How does hibernate do this?
Look here for something simple that does this:
http://jdbi.codehaus.org/
http://jakarta.apache.org/commons/dbutils/examples.html
Tom
Re: Derby with beans and null primitives
Posted by Thomas Dudziak <to...@gmail.com>.
On 11/30/05, Bryan Pendleton <bp...@amberpoint.com> wrote:
> Michael McCutcheon wrote:
> > I'm implementing a utility class that populates 'beans' with data from
> > tables in derby.
>
> Aren't you sort of re-inventing the wheel? It seems like there are a
> lot of libraries already out there which do this already. Surely
> it would be easier to just use an existing library?
>
> Try Google-searching for strings like JDO, Hibernate, Castor, etc.
Or perhaps jDBI or commons-dbutils, might be easier for his purposes.
Tom
Re: Derby with beans and null primitives
Posted by Michael McCutcheon <mi...@frontiernet.net>.
Bryan Pendleton wrote:
> Michael McCutcheon wrote:
>
>> I'm implementing a utility class that populates 'beans' with data
>> from tables in derby.
>
>
> Aren't you sort of re-inventing the wheel? It seems like there are a
> lot of libraries already out there which do this already. Surely
> it would be easier to just use an existing library?
>
> Try Google-searching for strings like JDO, Hibernate, Castor, etc.
>
> thanks,
>
> bryan
>
>
>
>
Yes, I have heard of these, but there is a HUGE learning curve to
these. What I need to do is fairly simple and does not require a
complete 'persistence solution'. I just need to populate beans from
resultsets and need to know how to handle null database integers with
java primitives.
How does hibernate do this?
Returning 0 will not work because 0 is different than null.
Thanks
Mike
Re: Derby with beans and null primitives
Posted by Bryan Pendleton <bp...@amberpoint.com>.
Michael McCutcheon wrote:
> I'm implementing a utility class that populates 'beans' with data from
> tables in derby.
Aren't you sort of re-inventing the wheel? It seems like there are a
lot of libraries already out there which do this already. Surely
it would be easier to just use an existing library?
Try Google-searching for strings like JDO, Hibernate, Castor, etc.
thanks,
bryan
Re: Derby with beans and null primitives
Posted by Lars Clausen <lc...@statsbiblioteket.dk>.
On Wed, 2005-11-30 at 06:28, Michael McCutcheon wrote:
> I'm implementing a utility class that populates 'beans' with data from
> tables in derby. Suppose I have the following 'bean':
>
> public class Person
> {
> private String firstName;
> private String lastName;
> private int weight;
>
> public void setWeight(int inWeight)
> {
> this.weight = inWeight;
> }
> public int getWeight()
> {
> return this.weight;
> }
> }
>
> I select out of a Person table to get the information to populate this bean.
>
> But what happens if weight is nullable in the table?
>
> Since you can't put nulls into primitives in Java, how is this handled?
>
> This is a real problem because 0 is definately different than no value.
>
> Also is there some standard for how JSP's deal with beans with primitive
> types?
>
> How should null primitives be handled in beans like this?
In Java 1.5, you could declare weight as an Integer and make use of the
autoboxing feature. Don't know how that interacts with beans, though.
-Lars
Re: Derby with beans and null primitives
Posted by Craig L Russell <Cr...@Sun.COM>.
Hi,
I think your issue is that you are using the wrong representation for
the data.
If you have nullable columns, map them to Integer.
If you have non-nullable columns, map them to int.
Otherwise, you will have this issue. If you are stuck with an
inflexible object model and an inflexible database schema, then you
have to have a policy in place for handling the mismatch. That is,
how to store a null Integer into a non-nullable column, and how to
store a null column value into an int. Your choices typically are to
throw an exception or to represent null as zero. If you choose to
represent null as zero you lose information. But this is an
application policy choice.
Craig
On Nov 29, 2005, at 9:28 PM, Michael McCutcheon wrote:
> I'm implementing a utility class that populates 'beans' with data
> from tables in derby. Suppose I have the following 'bean':
>
> public class Person
> {
> private String firstName;
> private String lastName;
> private int weight;
> public void setWeight(int inWeight)
> {
> this.weight = inWeight;
> }
> public int getWeight()
> {
> return this.weight;
> }
> }
>
> I select out of a Person table to get the information to populate
> this bean.
>
> But what happens if weight is nullable in the table?
>
> Since you can't put nulls into primitives in Java, how is this
> handled?
> This is a real problem because 0 is definately different than no
> value.
>
> Also is there some standard for how JSP's deal with beans with
> primitive types?
>
> How should null primitives be handled in beans like this?
>
> Thanks
>
> Mike
>
Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!