0

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> {
}
  • Not sure if this is related to the problem, but consider changing your repository to a `CrudRepository` , because the second parameter should be the type of the id column. – Arnaud Jul 05 '19 at 07:44
  • @Arnaud thank you for noticing that. Unfortunately i did not resolve the problem – mpDeveloped Jul 05 '19 at 07:54

2 Answers2

2

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • MSSQL is the database. Probably that is the issue. I have seen that there is different class Column on master branch on github( https://github.com/spring-projects/spring-data-jdbc/blob/master/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java ). Thank you very much for the answer. – mpDeveloped Jul 05 '19 at 14:20
-1

It seems that you haven't set id generation Strategy for the ID. Add this and try if it is working.

@GeneratedValue(strategy = GenerationType.AUTO) 
Rowi
  • 545
  • 3
  • 9