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

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

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

問題描述

我在玩 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?

推薦答案

這與事務(wù)不工作無關(guān).相反,它是關(guān)于 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.您應(yīng)該看到 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.

默認(rèn)情況下,當(dāng)一個實體的 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 設(shè)置為 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 將設(shè)置 id,即使它在您的實體中是最終的.

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

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 進(jìn)行直接插入,無需檢查 id,請參閱 https://jira.spring.io/browse/DATAJDBC-282.當(dāng)然,您可以在存儲庫的自定義方法中執(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

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

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

相關(guā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) - 簡單問題)
主站蜘蛛池模板: 自拍在线 | 国产精品久久久久久网站 | 国产色黄| 精品国产乱码一区二区三区a | 国产不卡一区 | 午夜视频网站 | 91日b| 国产一级片一区二区三区 | 午夜小电影 | 天天射天天干 | 久久久久亚洲精品中文字幕 | 欧美精品一区三区 | 国产精品精品 | 欧美一级视频在线观看 | 国产精品久久久久一区二区三区 | 欧美一二精品 | 亚洲激情av | 亚洲欧美日本国产 | 日日夜夜天天久久 | 亚洲精品一区二区网址 | 欧日韩在线观看 | 久久综合一区二区 | 欧美日韩三区 | 亚洲风情在线观看 | 欧美乱做爰xxxⅹ久久久 | 久久久蜜臀国产一区二区 | 成人在线视频网 | 激情av | 久久精品欧美一区二区三区麻豆 | 欧美午夜精品久久久久久浪潮 | 久热中文字幕 | 99精品久久久 | 亚洲欧美日韩国产 | 亚洲精品成人av | 亚洲欧美精品在线观看 | 中文字幕亚洲一区 | 日韩靠逼| 国产婷婷精品av在线 | 嫩草黄色影院 | 一级大片 | 97精品国产97久久久久久免费 |