We had the same problem and found the easiest workaround was to
implement the org.apache.ojb.broker.accesslayer.conversions.FieldConversion interface
to return null to the database if the primitive type was 0. Here is what
we use for ints (I know this works with hsqldb,oracle and m$ sqlserver):
import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
/**
* Converts int to Integer, Integer to int. If the int is zero, then
* we don't have a value for the key and it should be set to null, not
* zero.
*/
public class Int2IntegerFieldConversion implements FieldConversion{
private static final Integer intObj = null;
private static final Integer zero = new Integer(0);
public Object javaToSql(Object obj) throws ConversionException {
if( obj instanceof Integer ) {
Integer instance =(Integer)obj;
if(instance.equals(zero)){
return intObj;
} else {
return obj;
}
} else {
return obj;
}
}
public Object sqlToJava( Object obj ) throws ConversionException {
return obj;
}
}
Here is a snippit from my repository.xml that uses the above code:
<!-- Object property is a int, use conversion to get past foreign key
issues with primitives since we don't want to use wrappers -->
<field-descriptor id="11"
name="createdByID"
column="create_user_id"
jdbc-type="INTEGER"
conversion="org.ifmc.cart.common.Int2IntegerFieldConversion"/>
By doing this we didn't have to change any OJB code and we didn't
have to modify any of our dataobjects props from primitives to wrappers.
Hopefully this will help clear up the null reference problems that keep
coming up on this list, if not, it at least gives you another option. ;-)
The conversion doc goes into a little more detail:
http://jakarta.apache.org/ojb/jdbc-types.html
Aaron
On Mon, 30 Sep 2002, Philippe wrote:
> Hi,
>
> I encountered the null reference problem like described in
>http://archives.apache.org/eyebrowse/ReadMsg?[EMAIL PROTECTED]&msgNo=1126.
>
>
> By reading these messages archive, I see two solutions to solve this problem :
>
> 1. use wrappers (see
>http://archives.apache.org/eyebrowse/ReadMsg?[EMAIL PROTECTED]&msgNo=1612
> ). Some people are saying that Long is a solution
>http://archives.apache.org/eyebrowse/ReadMsg?[EMAIL PROTECTED]&msgNo=1614
> and
>http://archives.apache.org/eyebrowse/ReadMsg?[EMAIL PROTECTED]&msgNo=1616,
> other ones are saying BigDecimal's need to be used.
>
> 2. modify the code ( see
>http://archives.apache.org/eyebrowse/ReadMsg?[EMAIL PROTECTED]&msgId=466546)
> to take null's into account.
>
>
> I have three questions :
> a. WHICH wrapper is going to work : Integer, Long, BigDecimal and/or an other one ?
> b. what are the advantages, disadvantages of both solutions ?
> c. shall a new release of OJB contain support for null in Integer's or int's ?
>
> Thanks for helping me to solve this dilemma.
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>