[英]spring jdbc and composite primary keys
在插入行時,spring jdbc中是否有一種方法可以返回復合主鍵。 此復合主鍵由來自單獨序列的值組成
任何幫助是極大的贊賞
關心Damien
這是一個完整的例子(在PostgreSQL 8.4上測試):
我的桌子:
CREATE TABLE test
(
id serial NOT NULL,
otherid serial NOT NULL,
val text,
CONSTRAINT test_pkey PRIMARY KEY (id, otherid)
)
這是你獲得密鑰的方式:
public void doStuff() {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("insert into test(val) values (?)", Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, 42);
return ps;
}
},
keyHolder);
keyHolder.getKeys().get("id");
keyHolder.getKeys().get("otherid");
}
現在,如果您想直接從keyHolder獲取復合鍵作為某個類的實例,那么這並不簡單。
JdbcTemplate使用ColumnMapRowMapper來映射生成的鍵(生成的鍵作為結果集返回,至少在PostgreSQL上。它實際上返回整行,就好像您在剛剛插入的行上執行select一樣)。 相同的ColumnMapRowMapper用於JdbcTemplate中的其他位置。
這里唯一可能的擴展點是KeyHolder本身。 這是你可以做的:
public void doStuff() {
CompositeKeyHolder keyHolder = new CompositeKeyHolder();
... same code here ...
keyHolder.getCompositeKey();
}
class CompositeKeyHolder extends GeneratedKeyHolder {
private boolean converted;
public CompositeKey getCompositeKey() {
return new CompositeKey((Integer)this.getKeys().get("id"), (Integer)this.getKeys().get("otherid"));
}
}
class CompositeKey {
private Integer id;
private Integer otherId;
CompositeKey(Integer id, Integer otherId) {
this.id = id;
this.otherId = otherId;
}
public Integer getId() {
return id;
}
public Integer getOtherId() {
return otherId;
}
}
這是單個鍵的基本思想。 最后的長id是關鍵。 如果你有多個序列,我建議只使用兩個單獨的語句來獲取每個生成的密鑰。
JdbcTemplate template = getJdbcTemplate();
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(...);
return ps;
}
},
keyHolder);
long id = keyHolder.getKey().longValue();
你使用什么數據庫服務器? MySQL每個表只允許一個auto_increment字段,我想這通常就是這種情況,但不知道你的設置很難說。 假設表中只有一個auto_generated字段,那么INSERT必須知道進入第二個PK字段的值。 Robert的代碼應該用於檢索生成的鍵值,而最干凈的解決方案可能是在使用此生成的鍵和已經擁有的值之后執行SELECT。
我認為你需要的是GeneratedKeyHolder.getKeys() 。 代碼看起來像這個例子 ,除了你必須打電話
keyHolder.getKeys()
代替
keyHolder.getKey()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.