I am implementing a composite primary key with auto-generated annotation in spring boot hibernate. below is the scenario:
- Account table has below columns:
stateCode,branchCode,prodCode,subProdCode,accountNumber
whenever there is a change in stateCode,branchCode,prodCode,subProdCode , the table Account should have new accountNumber .
eg:
stateCode,branchCode,prodCode,subProdCode,accountNumber
11,01,20,1,00001
11,01,30,1,00001 (there is a change in prodCode)
11,01,30,2,00001 (there is a change in subprodcode)
11,01,20,2,00001 (there is a change in prod & subprodcode)
11,01,20,1,00002 (prodcode,subprod code has already account number 00001 ,
now it should be 00002)
the same question is already posted at Sequences with composite primary key
but i would like to know , if there is any new feature introduced in the latest version of spring boot hibernate.
Below is the code , which i have implemented:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@IdClass(CompositeId.class)
public class CompositeAccountNumber {
private String city;
private String custName;
@Id
@GeneratedValue
private Long accNumber;
@Id
private String stateCode;
@Id
private String branchCode;
@Id
private String prodCode;
@Id
private String subProdCode;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class CompositeId implements Serializable {
private String stateCode;
private String branchCode;
private String prodCode;
private String subProdCode;
private Long accNumber;
}
I am getting below result with above code:
stateCode,branchCode,prodCode,subProdCode,accountNumber
11,01,20,1,00001
11,01,30,1,00002
11,01,30,2,00003
11,01,20,2,00004
11,01,20,1,00005