問題描述
我在玩 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
或原始類型(例如 int
或 long
) 和 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:
將 id 設(shè)置為
null
并配置您的數(shù)據(jù)庫模式,以便在插入時自動創(chuàng)建一個新值.保存后,您的實體實例將包含從數(shù)據(jù)庫生成的值.
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)!