久久久久久久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 并發(fā)現(xiàn)了一個問題,我無法使用 Google 解決.

無論我嘗試做什么,我都無法將一個瑣碎的對象推送到數(shù)據(jù)庫中(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 數(shù)據(jù)庫(顯然)不會提交記錄.

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

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

這是(簡約的)源代碼: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

誰能告訴我,為什么汽車沒有插入數(shù)據(jù)庫?

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

推薦答案

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

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 并配置您的數(shù)據(jù)庫模式,以便在插入時自動創(chuàng)建一個新值.保存后,您的實體實例將包含從數(shù)據(jù)庫生成的值.

  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.

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

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.當然,您可以在存儲庫的自定義方法中執(zhí)行此操作,如本示例中所做的那樣: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模板網(wǎng)!

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
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 數(shù)據(jù)庫)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡單問題)
主站蜘蛛池模板: 国产日韩欧美在线观看 | 99久久精品国产麻豆演员表 | 日韩精品极品视频在线观看免费 | 久久久久久成人 | 久草视频在线播放 | 中文字幕在线第一页 | 黄篇网址| 欧美色影院 | 亚洲精品乱码久久久久久久久久 | 99热精品在线观看 | 国产成人99久久亚洲综合精品 | 99久久精品一区二区毛片吞精 | 国内精品久久久久 | 久久久久亚洲精品中文字幕 | www.788.com色淫免费 | 亚洲高清视频一区二区 | 亚洲综合色视频在线观看 | 成人自拍视频 | 日韩午夜电影 | 欧美色a v | 国产成人精品免高潮在线观看 | 99久久视频| 欧美激情视频网站 | 亚洲国产成人精品女人久久久 | 免费视频色 | 国产探花在线精品一区二区 | 日本激情视频在线播放 | 97免费在线观看视频 | 日韩a在线| 免费在线观看一区二区 | 亚洲精品中文在线 | 黄网站免费在线看 | 黄色免费观看网站 | 久久日韩粉嫩一区二区三区 | 91在线看片 | 久久国产精品99久久久久久丝袜 | 亚洲免费精品一区 | 国产精品久久久久久婷婷天堂 | 成人免费视频7777777 | 国产精品一区二区不卡 | 亚洲h色|