为什么Spring-data-jdbc不保存我的Car对象? [英] Why does Spring-data-jdbc not save my Car object?

查看:245
本文介绍了为什么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.

默认情况下,当Spring Data JDBC具有对象类型的ID和值null或原始类型(例如intlong)且值0.

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.

要使其正常运行,您有以下选择:

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.

注意:即使在您的实体中为最终ID,Spring Data JDBC也会设置ID.

保留ID null,然后在保存前"侦听器中将其设置为所需的值.

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对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆