久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 Spring-data-jdbc 不保存我的 Car 對象?

Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
本文介紹了為什么 Spring-data-jdbc 不保存我的 Car 對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在玩 spring-data-jdbc 并發現了一個問題,我無法使用 Google 解決.

無論我嘗試做什么,我都無法將一個瑣碎的對象推送到數據庫中(Bean1.java:25):carRepository.save(new Car(2L, "BMW", "5"));

No matter what I try to do, I just can't push a trivial object into the database (Bean1.java:25): carRepository.save(new Car(2L, "BMW", "5"));

兩者,沒有一個和一個 TransactionManager +@Transactional 數據庫(顯然)不會提交記錄.

Both, without one and with a TransactionManager +@Transactional the database (apparently) does not commit the record.

代碼基于 Postgres 數據庫,但您也可以簡單地使用下面的 H2 并獲得相同的結果.

這是(簡約的)源代碼:https://github.com/bitmagier/spring-data-jdbc-sandbox/tree/stackoverflow-question

Here is the (minimalistic) source code: https://github.com/bitmagier/spring-data-jdbc-sandbox/tree/stackoverflow-question

誰能告訴我,為什么汽車沒有插入數據庫?

Can somebody tell me, why the car is not inserted into the database?

推薦答案

這與事務不工作無關.相反,它是關于 Spring Data JDBC,考慮到您的實例是需要更新(而不是插入)的現有實例.

This is not related to transactions not working. Instead, it's about Spring Data JDBC considering your instance an existing instance that needs updating (instead of inserting).

您可以通過激活日志記錄 用于 org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.您應該看到 update 但沒有 insert.

You can verify this is the problem by activating logging for org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate. You should see an update but no insert.

默認情況下,當一個實體的 id 為對象類型且值為 null 或原始類型(例如 intlong) 和 0 的值.如果您的實體具有帶有 @Version 注釋的屬性,則該屬性將用于確定實例是否為新實例.

By default, Spring Data JDBC considers an entity as new when it has an id of an object type and a value of null or of a primitive type (e.g. int or long) and a value of 0. If your entity has an attribute with @Version annotation that attribute will be used to determine if the instance is a new one.

為了使其工作,您有以下選擇:

You have the following options in order to make it work:

  1. 將 id 設置為 null 并配置您的數據庫模式,以便在插入時自動創建一個新值.保存后,您的實體實例將包含從數據庫生成的值.

  1. Set the id to null and configure your database schema so that it will automatically create a new value on insert. After the save your entity instance will contain the generated value from the database.

注意:Spring Data JDBC 將設置 id,即使它在您的實體中是最終的.

保留 id null 并在 Before-Save 偵聽器中將其設置為所需的值.

Leave the id null and set it in a Before-Save listener to the desired value.

讓你的實體實現 可持久的.這允許您控制何時將實體視為.您可能還需要一個偵聽器,以便讓實體知道它不再是新的.

Let your entity implement Persistable. This allows you to control when an entity is considered new. You'll probably need a listener as well so you can let the entity know it is not new any longer.

從 Spring Data JDBC 1.1 版開始,您還可以使用 JdbcAggregateTemplate 進行直接插入,無需檢查 id,請參閱 https://jira.spring.io/browse/DATAJDBC-282.當然,您可以在存儲庫的自定義方法中執行此操作,如本示例中所做的那樣:https://github.com/spring-projects/spring-data-examples/pull/441

Beginning with version 1.1 of Spring Data JDBC you'll also be able to use a JdbcAggregateTemplate to do a direct insert, without inspecting the id, see https://jira.spring.io/browse/DATAJDBC-282. Of course, you can do that in a custom method of your repository, as is done in this example: https://github.com/spring-projects/spring-data-examples/pull/441

這篇關于為什么 Spring-data-jdbc 不保存我的 Car 對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡單問題)
主站蜘蛛池模板: 一区二区日本 | 日韩成人 | 一区二区三区高清 | 婷婷成人在线 | 国产精品99久久免费观看 | 天堂va在线 | 久久久精品一区二区三区四季av | 亚洲免费精品 | 亚洲自拍一区在线观看 | 综合色久 | 播放一级黄色片 | 亚洲精品中文字幕 | 国产在线色 | 综合久久综合久久 | 一区二区三区四区在线视频 | 成人在线中文字幕 | 亚洲高清av在线 | 黄网在线观看 | 欧美一级二级视频 | 依人成人 | 久久久久国产精品 | 精品国产乱码久久久久久蜜柚 | 欧美一级片在线 | 国产一区二区三区www | 久久精品久久精品 | 久久久久久毛片免费观看 | 婷婷激情综合 | 日本一区二区三区免费观看 | 免费h在线 | 综合久久久 | 在线免费观看成年人视频 | 九色 在线| 人人爽人人爽 | 女女百合av大片一区二区三区九县 | 欧美精品久久 | 一a一片一级一片啪啪 | www.天天操 | 91麻豆精品国产91久久久更新资源速度超快 | а天堂中文最新一区二区三区 | 亚洲美女网站 | 天天插天天操 |