Thread: [OJB-developers] Detecting and mapping null Integer values
Brought to you by:
thma
From: Darren S. <Dar...@cw...> - 2002-02-13 16:54:53
|
How can I detect null values for Integer, Long, Double etc Java class attributes ? Let's say we're mapping an object which has the following attributes: class Test { Integer id; String name; Integer sales; } Let's assume that both name and sales can be null. When retrieving this object from the database using the PersistenceBroker the String reference will be null but the sales reference will in fact refer to an Integer object with a zero value. If zero is within the valid range how can we detect that the value is actually null. This appears to boil down to: JdbcAccess.getObjectFromColumn. The code to convert a string looks like: case Types.VARCHAR: result = rs.getString(columnId); break; If the string value is null getString will return a null reference. But for Integer (and other classes) the following is used: case Types.INTEGER: result = new Integer(rs.getInt(columnId)); break; getInt will return 0 for null and the an integer will be created with a zero value. The Conversion Strategy stuff appears too late in the day to make the conversion. How is everybody else handling this or have I missed something ? Thanks Darren ****************************************************************************************************************************************************** This e-mail has been prepared using information believed by the author to be reliable and accurate, but Thales Information Systems Finance makes no warranty as to accuracy or completeness. In particular Thales Information Systems Finance does not accept responsibility for changes made to this e-mail after it was sent. Any opinions expressed in this document are those of the author and do not necessarily reflect the opinions of the company or its affiliates. They may be subject to change without notice. This e-mail, its content and any files transmitted with it are intended solely for the addressee(s) and may be legally privileged and/or confidential. Access by any other party is unauthorised without the express written permission of the sender. If you have received this e-mail in error you may not copy or use the contents, attachments or information in any way. Please destroy it and contact the sender via the Thales Information Systems Finance switchboard in London at +44 (0) 20 7650 0100 or via e-mail return. This message and any attachments have been scanned for viruses prior to leaving the originators network. The originator does not guarantee the security of this message and will not be responsible for any damages arising from any alteration of this message by a third party or as a result of any virus being passed on. ******************************************************************************************************************************************************* |
From: Thomas M. <tho...@ho...> - 2002-02-13 19:10:07
|
Hi Darren, Darren Syzling wrote: > How can I detect null values for Integer, Long, Double etc Java class attributes > ? Let's say we're mapping an object which has the following attributes: > > class Test > { > Integer id; > String name; > Integer sales; > } > > > Let's assume that both name and sales can be null. When retrieving this object > from the database using the PersistenceBroker the String reference will be null > but the sales reference will in fact refer to an Integer object with a zero > value. If zero is within the valid range how can we detect that the value is > actually null. > > This appears to boil down to: JdbcAccess.getObjectFromColumn. The code to > convert a string looks like: > > case Types.VARCHAR: > result = rs.getString(columnId); > break; > > If the string value is null getString will return a null reference. But for > Integer (and other classes) the following is used: > > case Types.INTEGER: > result = new Integer(rs.getInt(columnId)); > break; > I get your point. But: how should the above code look like to handle your problem correctly? I have no idea. If you (or somebody else) tell me how this section should look like I will change it! java.sql.ResultSet does not tell you that a column is NULL. It just returns a 0. Of course this is wrong. But SUN specified it this way. Or am I missing something? From the java.sql.ResultSet Source: /** * Gets the value of the designated column in the current row * of this <code>ResultSet</code> object as * an <code>int</code> in the Java programming language. * * @param columnIndex the first column is 1, the second is 2, ... * @return the column value; if the value is SQL <code>NULL</code>, * the value returned is <code>0</code> * @exception SQLException if a database access error occurs */ int getInt(int columnIndex) throws SQLException; > getInt will return 0 for null and the an integer will be created with a zero > value. > > The Conversion Strategy stuff appears too late in the day to make the > conversion. You are right, things are already on the wrong track at this point.... How is everybody else handling this or have I missed something ? cu, Thomas > > > Thanks > Darren > > > > ****************************************************************************************************************************************************** > This e-mail has been prepared using information believed by the author to be > reliable and accurate, but Thales Information Systems Finance makes no > warranty as to accuracy or completeness. In particular Thales Information > Systems Finance does not accept responsibility for changes made to this > e-mail after it was sent. Any opinions expressed in this document are those of > the author and do not necessarily reflect the opinions of the company or its > affiliates. They may be subject to change without notice. > This e-mail, its content and any files transmitted with it are intended > solely for the addressee(s) and may be legally privileged and/or > confidential. Access by any other party is unauthorised without the express > written permission of the sender. If you have received this e-mail in error you > may not copy or use the contents, attachments or information in any way. > Please destroy it and contact the sender via the Thales Information Systems > Finance switchboard in London at +44 (0) 20 7650 0100 or via e-mail return. > This message and any attachments have been scanned > for viruses prior to leaving the originators network. The originator does not > guarantee the security of this message and will not be responsible for any > damages arising from any alteration of this message by a third party or as a > result of any virus being passed on. > ******************************************************************************************************************************************************* > > > > _______________________________________________ > Objectbridge-developers mailing list > Obj...@li... > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > > > > |
From: Jakob B. <jbr...@ho...> - 2002-02-13 19:43:09
|
hi thomas, what about using wasNull of ResultSet ? /** * Reports whether * the last column read had a value of SQL <code>NULL</code>. * Note that you must first call one of the <code>getXXX</code> methods * on a column to try to read its value and then call * the method <code>wasNull</code> to see if the value read was * SQL <code>NULL</code>. * * @return <code>true</code> if the last column value read was SQL * <code>NULL</code> and <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean wasNull() throws SQLException; hth jakob ----- Original Message ----- From: "Thomas Mahler" <tho...@ho...> To: "Darren Syzling" <Dar...@cw...> Cc: <obj...@li...> Sent: Wednesday, February 13, 2002 8:09 PM Subject: Re: [OJB-developers] Detecting and mapping null Integer values > Hi Darren, > > Darren Syzling wrote: > > > How can I detect null values for Integer, Long, Double etc Java class attributes > > ? Let's say we're mapping an object which has the following attributes: > > > > class Test > > { > > Integer id; > > String name; > > Integer sales; > > } > > > > > > Let's assume that both name and sales can be null. When retrieving this object > > from the database using the PersistenceBroker the String reference will be null > > but the sales reference will in fact refer to an Integer object with a zero > > value. If zero is within the valid range how can we detect that the value is > > actually null. > > > > This appears to boil down to: JdbcAccess.getObjectFromColumn. The code to > > convert a string looks like: > > > > case Types.VARCHAR: > > result = rs.getString(columnId); > > break; > > > > If the string value is null getString will return a null reference. But for > > Integer (and other classes) the following is used: > > > > case Types.INTEGER: > > result = new Integer(rs.getInt(columnId)); > > break; > > > > > > I get your point. But: how should the above code look like to handle > your problem correctly? I have no idea. > If you (or somebody else) tell me how this section should look like I > will change it! > > > java.sql.ResultSet does not tell you that a column is NULL. It just > returns a 0. Of course this is wrong. But SUN specified it this way. > Or am I missing something? From the java.sql.ResultSet Source: > > /** > * Gets the value of the designated column in the current row > * of this <code>ResultSet</code> object as > * an <code>int</code> in the Java programming language. > * > * @param columnIndex the first column is 1, the second is 2, ... > * @return the column value; if the value is SQL <code>NULL</code>, > * the value returned is <code>0</code> > * @exception SQLException if a database access error occurs > */ > int getInt(int columnIndex) throws SQLException; > > > > getInt will return 0 for null and the an integer will be created with a zero > > value. > > > > The Conversion Strategy stuff appears too late in the day to make the > > conversion. > > > You are right, things are already on the wrong track at this point.... > > How is everybody else handling this or have I missed something ? > > > cu, > > Thomas > > > > > > > > Thanks > > Darren > > > > > > > > **************************************************************************** ************************************************************************** > > This e-mail has been prepared using information believed by the author to be > > reliable and accurate, but Thales Information Systems Finance makes no > > warranty as to accuracy or completeness. In particular Thales Information > > Systems Finance does not accept responsibility for changes made to this > > e-mail after it was sent. Any opinions expressed in this document are those of > > the author and do not necessarily reflect the opinions of the company or its > > affiliates. They may be subject to change without notice. > > This e-mail, its content and any files transmitted with it are intended > > solely for the addressee(s) and may be legally privileged and/or > > confidential. Access by any other party is unauthorised without the express > > written permission of the sender. If you have received this e-mail in error you > > may not copy or use the contents, attachments or information in any way. > > Please destroy it and contact the sender via the Thales Information Systems > > Finance switchboard in London at +44 (0) 20 7650 0100 or via e-mail return. > > This message and any attachments have been scanned > > for viruses prior to leaving the originators network. The originator does not > > guarantee the security of this message and will not be responsible for any > > damages arising from any alteration of this message by a third party or as a > > result of any virus being passed on. > > **************************************************************************** *************************************************************************** > > > > > > > > _______________________________________________ > > Objectbridge-developers mailing list > > Obj...@li... > > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > > > > > > > > > > > > > _______________________________________________ > Objectbridge-developers mailing list > Obj...@li... > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > |
From: Jakob B. <jbr...@ho...> - 2002-02-14 07:35:12
Attachments:
ojb_brj.zip
|
hi, the attached zip contains JdbcAccess using wasNull: case Types.INTEGER : { int val = rs.getInt(columnId); if (!rs.wasNull()) result = new Integer(val); break; } hth jakob ----- Original Message ----- From: "Jakob Braeuchi" <jbr...@ho...> To: "Thomas Mahler" <tho...@ho...>; "Darren Syzling" <Dar...@cw...> Cc: <obj...@li...> Sent: Wednesday, February 13, 2002 8:44 PM Subject: Re: [OJB-developers] Detecting and mapping null Integer values > hi thomas, > > what about using wasNull of ResultSet ? > > /** > * Reports whether > * the last column read had a value of SQL <code>NULL</code>. > * Note that you must first call one of the <code>getXXX</code> methods > * on a column to try to read its value and then call > * the method <code>wasNull</code> to see if the value read was > * SQL <code>NULL</code>. > * > * @return <code>true</code> if the last column value read was SQL > * <code>NULL</code> and <code>false</code> otherwise > * @exception SQLException if a database access error occurs > */ > boolean wasNull() throws SQLException; > > hth jakob > > ----- Original Message ----- > From: "Thomas Mahler" <tho...@ho...> > To: "Darren Syzling" <Dar...@cw...> > Cc: <obj...@li...> > Sent: Wednesday, February 13, 2002 8:09 PM > Subject: Re: [OJB-developers] Detecting and mapping null Integer values > > > > Hi Darren, > > > > Darren Syzling wrote: > > > > > How can I detect null values for Integer, Long, Double etc Java class > attributes > > > ? Let's say we're mapping an object which has the following attributes: > > > > > > class Test > > > { > > > Integer id; > > > String name; > > > Integer sales; > > > } > > > > > > > > > Let's assume that both name and sales can be null. When retrieving this > object > > > from the database using the PersistenceBroker the String reference will > be null > > > but the sales reference will in fact refer to an Integer object with a > zero > > > value. If zero is within the valid range how can we detect that the > value is > > > actually null. > > > > > > This appears to boil down to: JdbcAccess.getObjectFromColumn. The code > to > > > convert a string looks like: > > > > > > case Types.VARCHAR: > > > result = rs.getString(columnId); > > > break; > > > > > > If the string value is null getString will return a null reference. But > for > > > Integer (and other classes) the following is used: > > > > > > case Types.INTEGER: > > > result = new Integer(rs.getInt(columnId)); > > > break; > > > > > > > > > > > I get your point. But: how should the above code look like to handle > > your problem correctly? I have no idea. > > If you (or somebody else) tell me how this section should look like I > > will change it! > > > > > > java.sql.ResultSet does not tell you that a column is NULL. It just > > returns a 0. Of course this is wrong. But SUN specified it this way. > > Or am I missing something? From the java.sql.ResultSet Source: > > > > /** > > * Gets the value of the designated column in the current row > > * of this <code>ResultSet</code> object as > > * an <code>int</code> in the Java programming language. > > * > > * @param columnIndex the first column is 1, the second is 2, ... > > * @return the column value; if the value is SQL <code>NULL</code>, > > * the value returned is <code>0</code> > > * @exception SQLException if a database access error occurs > > */ > > int getInt(int columnIndex) throws SQLException; > > > > > > > getInt will return 0 for null and the an integer will be created with a > zero > > > value. > > > > > > The Conversion Strategy stuff appears too late in the day to make the > > > conversion. > > > > > > You are right, things are already on the wrong track at this point.... > > > > How is everybody else handling this or have I missed something ? > > > > > > cu, > > > > Thomas > > > > > > > > > > > > > Thanks > > > Darren > > > > > > > > > > > > > **************************************************************************** > ************************************************************************** > > > This e-mail has been prepared using information believed by the > author to be > > > reliable and accurate, but Thales Information Systems Finance makes no > > > warranty as to accuracy or completeness. In particular Thales > Information > > > Systems Finance does not accept responsibility for changes made to > this > > > e-mail after it was sent. Any opinions expressed in this document are > those of > > > the author and do not necessarily reflect the opinions of the company > or its > > > affiliates. They may be subject to change without notice. > > > This e-mail, its content and any files transmitted with it are > intended > > > solely for the addressee(s) and may be legally privileged > and/or > > > confidential. Access by any other party is unauthorised without the > express > > > written permission of the sender. If you have received this e-mail in > error you > > > may not copy or use the contents, attachments or information in any > way. > > > Please destroy it and contact the sender via the Thales Information > Systems > > > Finance switchboard in London at +44 (0) 20 7650 0100 or via e-mail > return. > > > This message and any attachments have been scanned > > > for viruses prior to leaving the originators network. The originator > does not > > > guarantee the security of this message and will not be responsible for > any > > > damages arising from any alteration of this message by a third party or > as a > > > result of any virus being passed on. > > > > **************************************************************************** > *************************************************************************** > > > > > > > > > > > > _______________________________________________ > > > Objectbridge-developers mailing list > > > Obj...@li... > > > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > Objectbridge-developers mailing list > > Obj...@li... > > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > > > > _______________________________________________ > Objectbridge-developers mailing list > Obj...@li... > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > |
From: Jakob B. <jbr...@ho...> - 2002-02-14 07:50:32
|
hi thomas, i think the method set() of PersistentField could be streamlined a lot when using f.set(obj,value) instead of the if-else-cascade. public void set(Object obj, Object value) throws IllegalAccessException, IllegalArgumentException { boolean before = getField().isAccessible(); AccessController.doPrivileged(new SetAccessibleAction()); Field f = getField(); Class type = f.getType(); try { f.set(obj,value); } catch (IllegalArgumentException ex) { getField().setAccessible(before); LoggerFactory.getDefaultLogger().error(ex); LoggerFactory.getDefaultLogger().error("field: " + fieldname + ", type: " + type); LoggerFactory.getDefaultLogger().error("value: " + value + ", type: " + value.getClass()); throw ex; } getField().setAccessible(before); } jakob ----- Original Message ----- From: "Thomas Mahler" <tho...@ho...> To: "Darren Syzling" <Dar...@cw...> Cc: <obj...@li...> Sent: Wednesday, February 13, 2002 8:09 PM Subject: Re: [OJB-developers] Detecting and mapping null Integer values > Hi Darren, > > Darren Syzling wrote: > > > How can I detect null values for Integer, Long, Double etc Java class attributes > > ? Let's say we're mapping an object which has the following attributes: > > > > class Test > > { > > Integer id; > > String name; > > Integer sales; > > } > > > > > > Let's assume that both name and sales can be null. When retrieving this object > > from the database using the PersistenceBroker the String reference will be null > > but the sales reference will in fact refer to an Integer object with a zero > > value. If zero is within the valid range how can we detect that the value is > > actually null. > > > > This appears to boil down to: JdbcAccess.getObjectFromColumn. The code to > > convert a string looks like: > > > > case Types.VARCHAR: > > result = rs.getString(columnId); > > break; > > > > If the string value is null getString will return a null reference. But for > > Integer (and other classes) the following is used: > > > > case Types.INTEGER: > > result = new Integer(rs.getInt(columnId)); > > break; > > > > > > I get your point. But: how should the above code look like to handle > your problem correctly? I have no idea. > If you (or somebody else) tell me how this section should look like I > will change it! > > > java.sql.ResultSet does not tell you that a column is NULL. It just > returns a 0. Of course this is wrong. But SUN specified it this way. > Or am I missing something? From the java.sql.ResultSet Source: > > /** > * Gets the value of the designated column in the current row > * of this <code>ResultSet</code> object as > * an <code>int</code> in the Java programming language. > * > * @param columnIndex the first column is 1, the second is 2, ... > * @return the column value; if the value is SQL <code>NULL</code>, > * the value returned is <code>0</code> > * @exception SQLException if a database access error occurs > */ > int getInt(int columnIndex) throws SQLException; > > > > getInt will return 0 for null and the an integer will be created with a zero > > value. > > > > The Conversion Strategy stuff appears too late in the day to make the > > conversion. > > > You are right, things are already on the wrong track at this point.... > > How is everybody else handling this or have I missed something ? > > > cu, > > Thomas > > > > > > > > Thanks > > Darren > > > > > > > > **************************************************************************** ************************************************************************** > > This e-mail has been prepared using information believed by the author to be > > reliable and accurate, but Thales Information Systems Finance makes no > > warranty as to accuracy or completeness. In particular Thales Information > > Systems Finance does not accept responsibility for changes made to this > > e-mail after it was sent. Any opinions expressed in this document are those of > > the author and do not necessarily reflect the opinions of the company or its > > affiliates. They may be subject to change without notice. > > This e-mail, its content and any files transmitted with it are intended > > solely for the addressee(s) and may be legally privileged and/or > > confidential. Access by any other party is unauthorised without the express > > written permission of the sender. If you have received this e-mail in error you > > may not copy or use the contents, attachments or information in any way. > > Please destroy it and contact the sender via the Thales Information Systems > > Finance switchboard in London at +44 (0) 20 7650 0100 or via e-mail return. > > This message and any attachments have been scanned > > for viruses prior to leaving the originators network. The originator does not > > guarantee the security of this message and will not be responsible for any > > damages arising from any alteration of this message by a third party or as a > > result of any virus being passed on. > > **************************************************************************** *************************************************************************** > > > > > > > > _______________________________________________ > > Objectbridge-developers mailing list > > Obj...@li... > > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > > > > > > > > > > > > > _______________________________________________ > Objectbridge-developers mailing list > Obj...@li... > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > |