Re: [PATCH] Re: [OJB-developers] NULL fields
Brought to you by:
thma
From: Thomas M. <tho...@ho...> - 2002-04-04 11:35:54
|
Hi Leandro, I just integrated your patch! cu, Thomas Leandro Rodrigo Saad Cruz wrote: > On Tue, 2002-04-02 at 17:34, Leandro Rodrigo Saad Cruz wrote: > >>Hi all, I have a problem with null fields >>Supose I have a FK from one table to another, sai this FK can be null, >>for example, to represent an inexistent association, when I do a : >> >> >>>query = new QueryByCriteria(CLASS-WITH-FK,null); >>>broker.getCollectionByQuery(query); >>> >>I get a NPE, is there any way to change this behavior ? >> > > I changed ojb.broker.metadata.PersistentFieldDefaultImpl to prevent NPEs > by testing if the value that will be assigned to the field by reflection > is null. I *really* don't know if the initial intention was throwing a > NPE, I think it's acceptable to prevent it. > > > > ------------------------------------------------------------------------ > > Index: src/ojb/broker/metadata/PersistentFieldDefaultImpl.java > =================================================================== > RCS file: /cvsroot/objectbridge/ojb-1-0/src/ojb/broker/metadata/PersistentFieldDefaultImpl.java,v > retrieving revision 1.2 > diff -u -b -B -r1.2 PersistentFieldDefaultImpl.java > --- src/ojb/broker/metadata/PersistentFieldDefaultImpl.java 25 Mar 2002 07:47:56 -0000 1.2 > +++ src/ojb/broker/metadata/PersistentFieldDefaultImpl.java 3 Apr 2002 15:06:35 -0000 > @@ -1,14 +1,7 @@ > -/* > - * Created by IntelliJ IDEA. > - * User: tom > - * Date: Aug 5, 2001 > - * Time: 10:12:23 PM > - * To change template for new class use > - * Code Style | Class Templates options (Tools | IDE Options). > - */ > package ojb.broker.metadata; > > import ojb.broker.util.LoggerFactory; > +import ojb.broker.util.Logger; > import ojb.broker.PersistenceBrokerException; > > import java.io.Serializable; > @@ -16,8 +9,11 @@ > import java.security.AccessController; > import java.security.PrivilegedAction; > > -public class PersistentFieldDefaultImpl implements PersistentField > +public class PersistentFieldDefaultImpl > + implements PersistentField > { > + private Logger logger = LoggerFactory.getLogger(getClass()); > + > public Field getField() > { > if (field == null) > @@ -60,9 +56,8 @@ > } > catch (Throwable t) > { > - LoggerFactory.getDefaultLogger().error("FieldDescriptor: Can't find member " + fieldname + " in " > - + c.getName()); > - LoggerFactory.getDefaultLogger().error(t); > + logger.error("FieldDescriptor: Can't find member " + fieldname + " in " + c.getName()); > + logger.error(t); > throw new PersistenceBrokerException(t); > } > } > @@ -102,23 +97,33 @@ > } > > > - public void set(Object obj, Object value) throws IllegalAccessException, IllegalArgumentException > + public void set(Object obj, Object value) > + throws IllegalAccessException, IllegalArgumentException > { > boolean before = getField().isAccessible(); > AccessController.doPrivileged(new SetAccessibleAction()); > Field f = this.getField(); > Class type = f.getType(); > + if(value != null) > + { > try > { > + > f.set(obj, value); > } > catch (IllegalArgumentException ex) > { > this.getField().setAccessible(before); > - LoggerFactory.getDefaultLogger().error(ex); > - LoggerFactory.getDefaultLogger().error("field: " + fieldname + ", type: " + type); > - LoggerFactory.getDefaultLogger().error("value: " + value + ", type: " + value.getClass()); > + logger.error("field: " + fieldname + ", type: " + type); > + logger.error("value: " + value + ", type: " + value.getClass()); > + logger.error(ex); > throw ex; > + } > +// catch(NullPointerException ignored) > +// { > +// //see Field javadoc > +// logger.info("Argument for field["+f.getName()+" is null"); > +// } > } > this.getField().setAccessible(before); > } > |