I am trying to utilize the following design for a class using the Spring Data, Hibernate, JPA frameworks. Note that the @GeneratedValue and @Id annoations are on different columns.
@Data
@Entity
@Table(name = "ACCTS")
public class MyAccount implements java.io.Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ACCOUNT_ID", nullable=false, precision=10, scale=0)
private long accountId;
@Id
@Column(name="ACCOUNT_NUMBER", unique=true, nullable=false, precision=9, scale=0)
private int accountNumber1;
@Column(name="PRIMARY_NUMBER", nullable=false, precision=9, scale=0)
private int accountNumber2;
}
The table looks like this. Note that the PRIMARY KEY constraint is not on the IDENTITY column.
CREATE TABLE CAACCTS (
ACCOUNT_ID BIGINT GENERATED BY DEFAULT AS IDENTITY ,
ACCOUNT_NUMBER DECIMAL(9, 0) DEFAULT 0 NOT NULL ,
PRIMARY_NUMBER DECIMAL(9, 0) DEFAULT 0 NOT NULL ,
CONSTRAINT XXX PRIMARY KEY( ACCOUNT_NUMBER ) ) ;
This table can be created, but JPA seems to have trouble with the IDENTITY column - the identity values are never created. Additionally, if I try to do an update, I get a "DUPLICATE PRIMARY KEY" error.
Has anybody successfully done this (splitting the IDENTITY and PRIMARY KEY columns) with JPA and Hibernate?