2017-12-10 6 views
1

BeanUtils.setPropertyメソッドを使用する際に問題があります。 私は、このJARを使用しています:BeanUtils.setPropertyメソッドは、BigDecimalフィールドをnullに設定します。

<dependency> 
    <groupId>commons-beanutils</groupId> 
    <artifactId>commons-beanutils</artifactId> 
    <version>1.9.3</version> 
</dependency> 

私は1つのレコードを返し、私は私が作ったのJavaBeanに結果セットをマッピングしていますMySQLのクエリを実行します。 ここにメインクラスがあります。

public class QueryTester { 

    public static void viewTable(Connection con) throws SQLException, InstantiationException, IllegalAccessException, InvocationTargetException { 
     Statement stmt = null; 
     String query = "SELECT * FROM Books WHERE code = 'AA00'"; 
     try { 
      stmt = (Statement) con.createStatement(); 
      ResultSet rs = stmt.executeQuery(query);  
      ResultSetMapper<Books> rsMapper = new ResultSetMapper<Books>(); 
      List<Books> list = rsMapper.mapResultSetToObject(rs, Books.class); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } finally { 
      if (stmt != null) { 
       stmt.close(); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     Connection conn = null; 
     String url = "jdbc:mysql://localhost/dbname"; 
     String driver = "com.mysql.jdbc.Driver"; 
     String userName = "root"; 
     String password = "root"; 
     try { 
      Class.forName(driver).newInstance(); 
      conn = (Connection) DriverManager.getConnection(url,userName,password); 
      viewTable(conn); 
      conn.close(); 
     } catch (Exception e) { 
      System.out.println("NO CONNECTION"); 
     } 
    } 
} 

これは、BeanUtils.setPropertyメソッドを使用するメソッドです。

public class ResultSetMapper<T> { 
    public List<T> mapResultSetToObject(ResultSet rs, Class<T> outputClass) throws InstantiationException, SQLException, IllegalAccessException, InvocationTargetException { 
     List<T> outputList = new ArrayList<T>(); 
     if (rs == null) { 
      return outputList; 
     } 
     if (!outputClass.isAnnotationPresent(Entity.class)) { 
      throw new InstantiationException("Entity notation not present."); 
     } 

     ResultSetMetaData rsmd = rs.getMetaData(); 
     // retrieve data fields from output class 
     Field[] fields = outputClass.getDeclaredFields(); 
     while (rs.next()) { 
      T bean = (T) outputClass.newInstance(); 
      for (int iterator = 0; iterator < rsmd.getColumnCount(); iterator++) { 
       String columnName = rsmd.getColumnName(iterator + 1); 
       Object columnValue = rs.getObject(iterator + 1); 
       for (Field field : fields) { 
        if (field.isAnnotationPresent(Column.class)) { 
         Column column = field.getAnnotation(Column.class); 
         if (column.name().equalsIgnoreCase(columnName) && columnValue != null) { 
          BeanUtils.setProperty(bean, field.getName(), columnValue); 
          break; 
         } 
        } 
       } 
      } 
      outputList.add(bean); 
     } 
     return outputList; 
    } 
} 

mapResultSetToObject方法は正しいですが、Beanは間違った方法で設定されている一つの要素を持つリストを返します。 フィールドcodeとbookDescriptionは正しく設定されていますが、データベースの値である3.000の代わりにkPriceフィールドがnullに設定されています。 このコードをデバッグモードで実行し、 "columnValue"変数の値は3.000ですが、setPropertyメソッドは正しい値を設定せず、値はnullのままです。

ここにはJava Beanがあります。

@Entity 
public class Books { 

    @Column(name="code") 
    private String code; 
    @Column(name="book_description") 
    private String bookDescription; 
    @Column(name="kPrice") 
    private BigDecimal kPrice; 

    public Books() {} 

    public Books(String code, String bookDescription, BigDecimal kPrice){ 
     this.code = code; 
     this.bookDescription = bookDescription; 
     this.kPrice = kPrice; 
    } 

    /* Getters and setters */ 
    ... 
} 

これはMySQLのテーブルとレコードです。

CREATE TABLE `Books` (
    `code` varchar(4) NOT NULL, 
    `book_description` varchar(50) NOT NULL DEFAULT '', 
    `kPrice` decimal(10,4) NOT NULL DEFAULT '1.0000', 
    PRIMARY KEY (`code`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

INSERT INTO dbname.Books (code, book_description, kPrice) VALUES('AA00', 'Description example', 3.0000); 

なぜこの現象が発生するのですか?私は何が欠けていますか? ありがとうございます

答えて

0

セッター/ゲッターの名前はプロパティと同じですか?

場合によっては、問題があります。

@Entity 
public class Books { 

    @Column(name="code") 
    private String code; 
    @Column(name="book_description") 
    private String bookDescription; 
    @Column(name="kPrice") 
    private BigDecimal kPrice; 

    public Books() {} 

    public Books(String code, String bookDescription, BigDecimal kPrice){ 
     this.code = code; 
     this.bookDescription = bookDescription; 
     this.kPrice = kPrice; 
    } 

    public void setKPrice (Bigdecimal kPrice) // and not setkPrice or setPrice.. 
    { 
     this.kPrice = kPrice; 
    } 

    public BigDecimal getKPrice() // and not getkPrice or getPrice.. 
    { 
     return this.kPrice; 
    } 
} 

以下の私の例を参照してください。

関連する問題