Tuesday, April 15, 2008

A quick update on JPA Boolean Magic Converter

Some update on changes:
  • Fixed the bug of Null pointer exception, when JPA return null on the annotated field.
  • Introduce a new properties call ifNull, which allows user to configure what to return if JPA returns null, it expect enum of org.jbpcc.util.jpa.ReturnType, which have values of ReturnType.True, ReturnType.FALSE, and ReturnType.Null,. The default value of ifNull is ReturnType.Null
Thus as an example, assuming we have model class define as below:

package org.jbpcc.domain.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import org.jbpcc.util.jpa.BooleanMagic;
import org.jbpcc.util.jpa.BooleanMagic.ReturnType;

@Entity
public class SomeVO {
@Id
private Integer id;
@BooleanMagic(trueValue = "Yes", falseValue = "No",
columnName = "OVERDUED", ifNull = ReturnType.FALSE)
private transient Boolean overdued;

public Boolean isOverdued() {
return overdued;
}

public void setOverdued(Boolean overdued) {
this.overdued = overdued;
}
}

Java APT with JPABooleanMagicConverter factory, code above will be converted to:

@Entity
public class SomeVO {
@Id
private Integer id;
private transient Boolean overdued;
//--- Lines below are generated by JBPCC BooleanMagicConvertor PROCESSOR
//--- START :

@Column(name="OVERDUED")
private String magicBooleanOverdued;


public Boolean isOverdued() {
if (this.magicBooleanOverdued == null)
return false;
return this.magicBooleanOverdued.equals("Yes") ? Boolean.TRUE : Boolean.FALSE;
}

public Boolean getOverdued() {
if (this.magicBooleanOverdued == null)
return false;
return this.magicBooleanOverdued.equals("Yes") ? Boolean.TRUE : Boolean.FALSE;
}

public void setOverdued(Boolean trueFlag) {
this.magicBooleanOverdued = trueFlag ? "Yes" : "No";
}
//--- END
//--- GENERATED BY JBPCC BooleanMagicConvertor PROCESSOR

}


That's all, as usual, you could find the JPABooleanMagicConverter binary at http://code.google.com/p/jbpcc/downloads/list.

Do share me your thoughts and comments, happy coding

1 comment:

Jess said...

Hi, keep up a good work, am actually like those technical posts, it is somehow helpful. If you have time, do keep it updated.

Keep up a good work!