Entities

Acc

@Entity @JsonIgnoreProperties({"remoteId", "owner"}) @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"uuid", "locale_id"})) public class Acc extends SynchronizedEntity implements Limit.OwnerOfLimit { private Long mId; @ManyToOne @JsonProperty("owner") private User mOwner; @JsonProperty("userId") private String mUserId; @JsonProperty("name") private String mName; @JsonProperty("cur") @ManyToOne(cascade = CascadeType.ALL) private Cur mCur; @JsonProperty("accType") @ManyToOne(cascade = CascadeType.ALL) private AccType mAccType; @JsonProperty("colorId") private String mColorId; @JsonProperty("rest") private Float mRest; @JsonProperty("order") private Long mOrder; @JsonProperty("limitOwner") private String limitOwner; 

Cur

 @Entity @Immutable public class Cur extends BaseEntity { @JsonProperty("iso") private String mIso; @JsonProperty("name") private String mName; @JsonProperty("use") private String mUse = "N"; } 

AccType

 @Entity @Immutable public class AccType extends BaseEntity { @JsonProperty("name") private String mName; @JsonProperty("ap") private short mAp; @JsonProperty("iconid") private String mIconid; } 

REST server ( Spring ) processes the request with the Acc object and performs the save action, updates, marks for deletion. I wanted to make the cur and AccType tables be reference books (there are only 3 fields in AccType and Cur 160 in CurType ). But now, when I try to save a freshly-sent object to the database through its DAO, the usual method save I get an error, namely

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n / a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: couldn’t execute statement Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates the uniqueness constraint "cur_pkey Details: Key" (id) = (2) "already exists.

As I understand it, hibernate is trying to write an object to the cur table and naturally cannot do this, since the field is already taken. BUT the bottom line is that it should not create a field, but should find the field already created in the directory and add it to Acc .

Is there any annotation that prohibits writing and says that you need to search the field for such data, and not create

  • If I’m not mistaken, then the @ManyToOne(cascade = CascadeType.ALL) needs to disable the cascade update. - Bleser
  • After deleting, it throws the object references an unsaved transient instance - save the transient instance before flushing But I don't need it to save the field, I need to pick up the ready one from the reference book - Sprytin

1 answer 1

Try

 @ManyToOne(cascade = CascadeType.MERGE)