我正在使用Hibernate和spring。這是我的模型類使用休眠時出現空指針異常
@Entity
@NamedNativeQueries({@NamedNativeQuery(
name = "CSI_TARGET",
query = "select * from CSITARGET('CSIINDEX',2)",
resultClass = CSITarget.class)})
public class CSITarget {
@Column(name="csi_target")
private BigDecimal csi_target;
@Id
@Column(name="financialyearfrom" ,nullable = true)
private int financialyearfrom =0;
@Column(name="at_yearhalf" , nullable = true)
private String at_yearhalf = "";
public BigDecimal getCsi_target() {
return csi_target;
}
public void setCsi_target(BigDecimal csi_target) {
this.csi_target = csi_target;
}
public int getFinancialyearfrom() {
return financialyearfrom;
}
public void setFinancialyearfrom(int financialyearfrom) {
this.financialyearfrom = financialyearfrom;
}
public String getAt_yearhalf() {
return at_yearhalf;
}
public void setAt_yearhalf(String at_yearhalf) {
this.at_yearhalf = at_yearhalf;
}
我正在使用Hibernate調用postgres數據庫中的存儲過程。存儲過程返回映射到此模型類的表。現在我的問題是,從數據庫返回的表包含一個空值。我需要對數據進行一些操作。現在,由於空值映射到bean類,我得到一個空指針異常。我如何讓hibernate忽略數據庫中的空值,併爲bean類中的相應屬性設置默認值。正如你所看到的,我也使用了可空屬性。它不起作用。
是'at_yearhalf'還是'financialyearfrom'' null'?那麼,他們被允許成爲。我建議你的邏輯爲可以有值的字段處理空值。另一個(不是很好的解決方案IMO)是使用getters和setter來檢查null並替換值。 – Magnilex 2013-05-06 10:51:55
感謝您的建議。我正在檢索數據列表。在這個列表中,例如第三個值(db中的記錄)爲空,那麼我得到了異常。如果我做了一些嘗試,我無法處理表中剩餘的數據。我可以嗎? – 2013-05-06 10:57:23