I have an entity class with a composite primary key that looks this:
@Entity
@Table(schema = "ANYBODY", name = "PERSON")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
@EmbeddidId
private PersonPK personPk;
@Column(name = "FIRST_NAME")
private firstName;
@Column(name = "LAST_NAME")
@Column lastName;
}
... and the composite primary key class:
@Embeddable
@Data
@NoArgsConstructor
public class PersonPK implments Serializable {
@NotNull
@Column(columnDefinition = "CHAR(4 BYTE)", name = "NICK_NAME")
private String nickname;
@NotNull
@Column(name = "AGE")
private Integer age;
}
My Oracle database schema looks like this:
CREATE TABLE ANYBODY.PERSON
(
FIRST_NAME VARCHAR2(30 BYTE) NOT NULL
, LAST_NAME VARCHAR2(30 BYTE) NOT NULL
, AGE NUMBER NOT NULL
, NICK_NAME CHAR(4 BYTE) NOT NULL
);
ALTER TABLE ANYBODY.PERSON ADD CONSTRAINT PERSON PRIMARY KEY
(
AGE
, NICK_NAME
);
I've found that I can use the JPA save()
method to create a new row with an entity that looks like this: new Person(new PersonPK("Bud", 40), "Nathan", "Reed")
However, if I try to use save() to update the row with: new Person(new PersonPK("Bud", 40), "Nate", "Reed")
, I get a org.hibernate.exception.ConstraintViolationException;
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (ANYBODY.PERSON) violated
If I create a new row using JPA save()
with an entity that looks like this: new Person(new PersonPK("Lily", 40), "Lilia", "Millar")
, (notice the nickname property on the primary key now has a string value of length 4), I can use JPA save() to update the row in
the Oracle database without any issues. I think it has something to do with the fact that I've defined the column with a type of CHAR(4 BYTE)
, but I don't understand why I
can use JPA save()
to create a row with a value for the NICK_NAME column that has a string of length 3, but I can't use JPA save()
to update that row.
So. Why I can't I use JPA save()
to update a row with a value for a string that has a length of 3, if the column type of a table in an Oracle database is CHAR(4 BYTE)
?
You are getting error because of Unique Constraint being broken
So, this data already exists
new Person(new PersonPK("Bud", 40), "Nathan", "Reed")
You must already bbe having an entry for
It is not the 4 byte issue
Try clear the table, then make the entry to confirm the same