我正在使用JdbcTemplate查询数据库的Spring应用程序,并且rowmapper的结果与查询的结果不同。
我的查询返回:
+--------------------+---------+------+------+----------+----------+----------+----------+---------+----------------+-----------------+
| ORIGINATING_SYSTEM | SYS_REF | CUR1 | CUR2 | TRADE_DT | START_DT | END_DT | BOOK_REF | BOOK_ID | NOMINAL | ORIGIN_VALUE_DT |
+--------------------+---------+------+------+----------+----------+----------+----------+---------+----------------+-----------------+
| CC | 4000000 | USD | | 01/04/19 | 01/04/19 | 01/04/19 | TDCZK | 317 | -8245872154,55 | 29/03/19 |
| GPS | 4000000 | EUR | | 01/04/19 | 28/03/19 | 28/03/19 | TDCZK | 317 | 55555550 | |
+--------------------+---------+------+------+----------+----------+----------+----------+---------+----------------+-----------------+
此结果由rowmapper处理以获取列表:
public List<Trade> findBackValueTrades() {
List<Trade> trades = getJdbcTemplate().query(FIND_BACK_VALUE_TRADES, new BackValueTradeMapper());
}
class BackValueTradeMapper implements RowMapper<Trade> {
public BackValueTradeMapper() {
}
@Override
public Trade mapRow(final ResultSet rs, final int rowNum) throws SQLException {
Trade trade = new Trade();
trade.setOriginatingSystem(rs.getString("ORIGINATING_SYSTEM"));
trade.setSystemRef(rs.getString("SYS_REF"));
trade.setNominal(rs.getDouble("NOMINAL"));
Currency cur1 = new Currency();
cur1.setId(rs.getString("CUR1"));
trade.setCurrency1(cur1);
Currency cur2 = new Currency();
cur2.setId(rs.getString("CUR2"));
trade.setCurrency2(cur2);
trade.setTradeDate(rs.getDate("TRADE_DT"));
trade.setStartDate(rs.getDate("START_DT"));
trade.setEndDate(rs.getDate("END_DT"));
Book book = new Book();
book.setBookRef(rs.getString("BOOK_REF"));
book.setId(rs.getLong("BOOK_ID"));
trade.setBook(book);
trade.setNominal(rs.getDouble("NOMINAL"));
trade.setEnteredDate(rs.getDate("ORIGIN_VALUE_DT"));
return trade;
}
}
其中贸易只是@Entity
,仅包含字段,获取器和设置器:
@Table(name = "TRADES")
@Entity
public class Trade implements Serializable {
private static final long serialVersionUID = 2143115773381859155L;
@Column(name = "ID")
@Id
private Long id;
@Column(name = "SYS_REF")
private String systemRef;
@Column(name = "ORIGINATING_SYSTEM")
private String originatingSystem;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CUR1")
private Currency cur1;
// remaining fields + getters/setters
}
问题出在结果列表中,似乎CC
的交易被覆盖了:
+--------------------+---------+------+------+----------+----------+----------+----------+---------+-----------+-----------------+
| ORIGINATING_SYSTEM | SYS_REF | CUR1 | CUR2 | TRADE_DT | START_DT | END_DT | BOOK_REF | BOOK_ID | NOMINAL | ORIGIN_VALUE_DT |
+--------------------+---------+------+------+----------+----------+----------+----------+---------+-----------+-----------------+
| GPS | 4000000 | EUR | | 01/04/19 | 28/03/19 | 28/03/19 | TDCZK | 317 | 55555550 | 29/03/19 |
| GPS | 4000000 | EUR | | 01/04/19 | 28/03/19 | 28/03/19 | TDCZK | 317 | 55555550 | 28/03/19 |
+--------------------+---------+------+------+----------+----------+----------+----------+---------+-----------+-----------------+
那是为什么?
顺便说一句,我能够通过扩展Trade
类并覆盖equals和hashCode来解决此问题,但是我想知道它为什么起作用。