Search code examples
javaspring-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 = ? ... WHERE id = ?

This raises exception Cannot update identity column 'id'

ID is generated by the database

used version: 1.0.6.RELEASE & 1.0.9.RELEASE

DB: mssql

Why is update statement trying to update the ID column as it is the primary key?

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> {
}

Solution

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

    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.