我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 页面中的编辑链接时,它通过使用支持 beans PaymentSpecVO.java 将 paymentID 传递给 PaymentSpecUIService.java 的 findPaymentSpecByKey(PaymentSpecId key) 方法,并且在弹出窗口中它将在 inputText 组件中显示 paymStartWK 值为 0,尽管DB中的数据存储为NULL