pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問(wèn)題描述

我在玩 spring-data-jdbc 并發(fā)現(xiàn)了一個(gè)問(wèn)題,我無(wú)法使用 Google 解決.

無(wú)論我嘗試做什么,我都無(wú)法將一個(gè)瑣碎的對(duì)象推送到數(shù)據(jù)庫(kù)中(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"));

兩者,沒(méi)有一個(gè)和一個(gè) TransactionManager +@Transactional 數(shù)據(jù)庫(kù)(顯然)不會(huì)提交記錄.

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

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

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

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

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

推薦答案

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

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).

您可以通過(guò)激活日志記錄 用于 org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.您應(yīng)該看到 update 但沒(méi)有 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)一個(gè)實(shí)體的 id 為對(duì)象類型且值為 null 或原始類型(例如 intlong) 和 0 的值.如果您的實(shí)體具有帶有 @Version 注釋的屬性,則該屬性將用于確定實(shí)例是否為新實(shí)例.

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

  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,即使它在您的實(shí)體中是最終的.

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

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

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

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 版開(kāi)始,您還可以使用 JdbcAggregateTemplate 進(jìn)行直接插入,無(wú)需檢查 id,請(qǐng)參閱 https://jira.spring.io/browse/DATAJDBC-282.當(dāng)然,您可以在存儲(chǔ)庫(kù)的自定義方法中執(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 對(duì)象?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡(jiǎn)單問(wèn)題)
主站蜘蛛池模板: 橡胶电子拉力机-塑料-微电脑电子拉力试验机厂家-江苏天源 | 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | 河北中仪伟创试验仪器有限公司是专业生产沥青,土工,水泥,混凝土等试验仪器的厂家,咨询电话:13373070969 | VOC检测仪-甲醛检测仪-气体报警器-气体检测仪厂家-深恒安科技有限公司 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 1000帧高速摄像机|工业高速相机厂家|科天健光电技术 | 北京西风东韵品牌与包装设计公司,创造视觉销售力! | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | TPU薄膜_TPU薄膜生产厂家_TPU热熔胶膜厂家定制_鑫亘环保科技(深圳)有限公司 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 嘉兴恒升声级计-湖南衡仪声级计-杭州爱华多功能声级计-上海邦沃仪器设备有限公司 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 茶叶百科网-茶叶知识与茶文化探讨分享平台 | 十二星座查询(性格特点分析、星座运势解读) - 玄米星座网 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | 活性炭-蜂窝-椰壳-柱状-粉状活性炭-河南唐达净水材料有限公司 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 专注氟塑料泵_衬氟泵_磁力泵_卧龙泵阀_化工泵专业品牌 - 梭川泵阀 | 全自动包衣机-无菌分装隔离器-浙江迦南科技股份有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 万家财经_财经新闻_在线财经资讯网| 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | 食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 粘度计NDJ-5S,粘度计NDJ-8S,越平水分测定仪-上海右一仪器有限公司 | 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 |