Answer the question
In order to leave comments, you need to log in
Why does CrudRepository#save() throw a duplicate entry?
In the process of studying the topic, I ran into a problem. There is a CrudRepository and this VersionedEntity:
@Entity
@Table(name = "version_table")
public class VersionedEntity {
@Id
@Column(name = "id")
private int id;
@Column(name = "first_field")
private String firstField;
@Column(name = "second_field")
private String secondField;
public VersionedEntity() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstField() {
return firstField;
}
public void setFirstField(String firstField) {
this.firstField = firstField;
}
public String getSecondField() {
return secondField;
}
public void setSecondField(String secondField) {
this.secondField = secondField;
}
@Override
public boolean equals(Object obj) {
return obj instanceof EntityRepository && ((VersionedEntity) obj).id == this.id;
}
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
EntityRepository repository = (EntityRepository) ctx.getBean("entityRepository");
VersionedEntity entity = repository.findOne(0);
entity.setFirstField("ffsv");
repository.save(entity);
}
Answer the question
In order to leave comments, you need to log in
The id must have the @GeneratedValue annotation:
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question