我在从数据库中检索Integer
值时遇到问题。我正在使用Spring Data JPA与数据库进行交互。对于可以为空的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);
}
我的数据库表
COLUMN_NAME DATA_TYPE NULLABLE DEFAULT_VALUE
------------ --------- -------- -------------
PAYM_START_WK NUMBER(4,0) Yes null
当我点击来自UI的编辑链接,即JSF页面时,它通过使用支持bean PaymentSpecVO.java将paymentID传递给PaymentSpecUIService.java的findPaymentSpecByKey(PaymentSpecId键)方法,并且在弹出窗口中它将在inputText组件中显示paymStartWK值为0,虽然DB中的数据存储为NULL