我有幾個 SQL 表說
table1
___________________________ _ _ _ _ _
|id | date | col1 | col2 |col3 |col4 ...
--------------------------------------
|1 | 1/11 | | | |
____________________________ _ _ _ _ _
和
table2
_________________________
|id | date | col1 | col2 |
--------------------------
|1 | 1/11 | ice | cone |
我希望能夠根據 id 和日期將 table2 中的資料合并到 table1 中 - 如果 id 和日期匹配 - 然后將資料(col1 和 col2)從 table2 復制到 table1 上。
我有 SQL 查詢來完成這項作業
// merge_query1:
MERGE table1 t1
USING table2 t2
ON (t1.id = t2.id AND t1.date=t2.date )
WHEN MATCHED
THEN UPDATE SET
t1.col1 = t2.col1,
t1.col2 = t2.col2
;
現在的問題是如何使用 Spring JPA 執行此操作。
我在想像這樣寫一個 JPA 介面的東西。
// How would I design this or something like this ?
// What pojo should be put instead of the question mark in JpaRepository<?, .. >
public interface MergeTables extends JpaRepository<?, Long> {
@Query(value = MERGE_TABLE2_WITH_TABLE1, nativeQuery = true)
void executeMergeTableQuery();
}
MERGE_TABLE2_WITH_TABLE1字串在哪里
public static String MERGE_TABLE2_WITH_TABLE1 = "MERGE table1 t1
USING table2 t2
ON (t1.id = t2.id AND t1.date=t2.date )
WHEN MATCHED
THEN UPDATE SET
t1.col1 = t2.col1,
t1.col2 = t2.col2
;
";
所以問題是我將如何設計以便在呼叫上述函式時執行合并查詢,或者是否有另一種方法來做這樣的事情?
uj5u.com熱心網友回復:
添加
@Modified并@Transactional成功了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/494639.html
