我知道BeanPropertyRowmapper
使用setter
的方法當我點火select
查詢但是使用getter
方法?BeanPropertyRowMapper如何在Spring內部工作?
我面臨的問題如下:
在database
,defaultPriority是string
但我想在我的SMSAction
POJO類設置int
值。
class SMSAction implements Serializable {
private int defaultPriority;
public int getDefaultPriority() {
System.out.println("Inside getDefaultPriority()");
return defaultPriority;
}
public void setDefaultPriority(String defaultPriority) {
System.out.println("Inside setDefaultPriority(String defaultPriority)"+defaultPriority);
if(defaultPriority.equalsIgnoreCase("L")){
System.out.println("Condition");
this.defaultPriority = 1;
}
}
}
,這是錯誤我得到:
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [select SMSACTIONID,SMSACTIONCODE,ACTIONDESC,CASID,DEFAULTPRIORITY from tblsmsaction]; SQL state [99999]; error code [17059]; Fail to convert to internal representation; nested exception is java.sql.SQLException: Fail to convert to internal representation
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
當數據類型是在數據庫和我的POJO不同,如何解決上述問題? 當我改變return type of getDefaultPriority()
到String
那麼它做工精細,但我不能明白,爲什麼BeanPropertyRowMapper
使用getDefaultPrioriy()
也是我log
印刷內部getDefaultPriority()
不顯示。
注意:我不想讓自定義行映射器或休眠或JPA。 請幫幫我。
這基本上與Spring沒有任何關係,但是與Java Bean規範有關,它指出getter/setter的類型必須匹配。 – 2015-01-21 11:49:02
@ M.Deinum目前我還沒有做過SMSAction(它的pojo類)的bean,所以爲什麼需要匹配getter/setter? – 2015-01-22 06:17:53
因爲這是它在Java Bean Specification中指定的方式,所以需要匹配getter/setter的類型。在設置Java Bean Spec應用的屬性時,它是否是一個bean並不重要。 – 2015-01-22 07:10:35