データベースからの値の取得に問題がありInteger
ます。Spring Data JPA を使用してデータベースとやり取りしています。NULL 可能なNUMBER
列の場合、データベース (Oracle) からの NULL 値が0
ではなくとして返されnull
ます。
コードは次のとおりです。
PaymentSpecVO.java
:
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private Integer paymStartWk;
public Integer getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk;
}
}
PaymentSpecUIService.java
public class PaymentSpecUIService implements IPaymentSpecUIService {
@Autowired
protected IPaymentSpecService paymentSpecService;
public void savePaymentSpec(PaymentSpecVO paymentSpecVO) {
//some logic
paymentSpec = paymentSpecService.save(paymentSpec);
// transform the saved entity back to VO object
//Here All my VO objects extends BaseVO
paymentSpecVO.toDTO(paymentSpec);
}
public PaymentSpecVO findPaymentSpecByKey(PaymentSpecId key) {
//Here during retrival It uses findByKey(key);
PaymentSpec paymentSpec = paymentSpecService.findByKey(key);
PaymentSpecVO paymentSpecVO = new PaymentSpecVO();
if (paymentSpec != null) {
paymentSpecVO.toDTO(paymentSpec);
return paymentSpecVO;
}
}
}
BaseVO.java
public abstract class BaseVO<E extends Object> implements Serializable {
private Class<E> entity;
protected BaseVO(Class<E> entity) {
this.entity = entity;
ConvertUtils.register(dateConverter, Date.class);
}
public Object toDTO(E entity) {
try {
copyEntityToValueObjectProperties(entity, this);
} catch (Exception ex) {
ex.printStackTrace();
}
return this;
}
/**
* Copies the properties from Entity object to Value object.
*
* */
public void copyEntityToValueObjectProperties(Object entityObject, Object valueObject) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,InstantiationException, SecurityException, NoSuchFieldException {
// Get the list of fields from source objects
Field[] entityObjectFields = entityObject.getClass().getDeclaredFields();
// Reference of BaseEntity class type
Class<?> entityType = BaseEntity.class;
Class<?> collType = Collection.class;
// Iteration of entity properties
for (Field entityField : entityObjectFields) {
try{
//
// Checks whether the entity field is an object and is of type
// BaseEntity class.
//
if (!entityType.isAssignableFrom(entityField.getType()) && !collType.isAssignableFrom(entityField.getType())
&& !Modifier.isFinal(entityField.getModifiers()) && !Modifier.isStatic(entityField.getModifiers())) {
BeanUtils.setProperty(valueObject, entityField.getName(), BeanUtils.getProperty(entityObject, entityField.getName()));
}
}
catch(Exception e){
continue;
}
}
}
}
PaymentSpecId.java
public class PaymentSpecId implements BaseEntity {
private String contractId;
private String sectionId;
private String id;
public PaymentSpecId() {
}
public PaymentSpecId(String id, String contractId, String sectionId){
this.id = id;
this.contractId = contractId;
this.sectionId = sectionId;
}
@Column(name = "ID", nullable = false, length = 2)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
}
IPaymentSpecService.java
@Transactional(readOnly = true)
public interface IPaymentSpecService {
@Transactional(readOnly = false)
public PaymentSpec save(PaymentSpec PaymentSpec);
}
私のDBテーブル
COLUMN_NAME DATA_TYPE NULLABLE DEFAULT_VALUE
------------ --------- -------- -------------
PAYM_START_WK NUMBER(4,0) Yes null
そして、UI、つまり JSF ページから編集リンクをクリックすると、バッキング Bean PaymentSpecVO.java を使用して PaymentSpecUIService.java の findPaymentSpecByKey(PaymentSpecId key) メソッドに paymentID が渡され、ポップアップでは、inputText コンポーネントの paymStartWK 値が 0 として表示されますが、 DB のデータは NULL として格納されます