简体   繁体   English

带有存储库保存方法更新ID的spring-data-jdbc更新

[英]spring-data-jdbc update with repository save methods updates ID

Calling CrudRepository save() method for an entity that is NOT new creates following sql: UPDATE card SET id = ?, customer_id = ? 为不是新实体的实体调用CrudRepository save()方法将创建以下sql: UPDATE card SET id =?,customer_id =? ... WHERE id = ? ...哪里id =?

This raises exception Cannot update identity column 'id' 这引发异常无法更新标识列“ id”

ID is generated by the database ID由数据库生成

used version: 1.0.6.RELEASE & 1.0.9.RELEASE 使用的版本:1.0.6.RELEASE和1.0.9.RELEASE

DB: mssql DB:mssql

Why is update statement trying to update the ID column as it is the primary key? 为什么update语句试图更新ID列,因为它是主键?

Entity: 实体:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

@Table("card")
public class Card {

    @Id
    private Long id;

    @Column("customer_id")
    private String customerId;
...

Repository: 仓库:

public interface CardRepository extends CrudRepository<Card, String> {
}

This sounds like https://jira.spring.io/browse/DATAJDBC-262 which was fixed and released in version 1.1.M1 the current version of that development branch is 1.1.RC1 . 这听起来像https://jira.spring.io/browse/DATAJDBC-262 ,它已在1.1.M1版本中修复并发布,该开发分支的当前版本为1.1.RC1

Switching to that version should solve the problem. 切换到该版本应该可以解决问题。

Note: I've seen the exception you mention only with MS-SqlServer which isn't yet fully supported yet. 注意:我已经看到您只提到了MS-SqlServer的例外情况,但尚未完全支持。

It seems that you haven't set id generation Strategy for the ID. 似乎您尚未为ID设置ID生成策略。 Add this and try if it is working. 添加此内容,然后尝试是否可行。

@GeneratedValue(strategy = GenerationType.AUTO) 

If an entity has a primary key field that is not marked with @GeneratedValue, the automatic primary key value is not generated and the application is responsible to set a primary key by initializing the primary key field. 如果实体的主键字段未标记@GeneratedValue,则不会生成自动主键值,并且应用程序负责通过初始化主键字段来设置主键。 That must be done before any attempt to persist the entity object. 必须在尝试持久化实体对象之前完成该操作。 Also, I think you need to implement Hashcode and Equals for the Card entity. 另外,我认为您需要为Card实体实现Hashcode和Equals。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM