I have an entity class SupplierOrder.java which contains two fields: cuId and orderId
@Data
@EqualsAndHashCode(callSuper = false, of = { "cuId" })
@Builder(toBuilder = true)
@AllArgsConstructor
@Entity
@Table(name = "supplier_order")
public class SupplierOrder
{
@Column(name = "cuid", nullable = false)
private Long cuId;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "supOrd")
@SequenceGenerator(name = "supOrd", sequenceName = "supplier_order_seq", allocationSize = 1 )
@Column(name = "order_id", nullable = false)
private Integer orderId;
}
When I save my entity the following operation on the database used to undertake
1. select next value for supplier_order_seq
2. insert into supplier_order(cuid,order_id)values(101,"<value from the point 1>")
Now, I have to flip the primary key. In other words, cuid becomes my new primary key and the rest remains the same.
@Data
@EqualsAndHashCode(callSuper = false, of = { "orderId" })
@Builder(toBuilder = true)
@AllArgsConstructor
@Entity
@Table(name = "supplier_order")
public class SupplierOrder
{
@Id //changed primary key from orderId to cuid
@Column(name = "cuid", nullable = false)
private Long cuId;
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "supOrd")
@SequenceGenerator(name = "supOrd", sequenceName = "supplier_order_seq", allocationSize = 1 )
@Column(name = "order_id", nullable = false)
private Integer orderId;
}
Now, when I am trying to save my entity the following operations happen:
1. insert into supplier_order(cuid,order_id)values(101,NULL)
Note: getting next value from the seq supplier_order_seq does not happen and because of this, it is giving me error as I cannot save order_id as null. How do I make sure that order_id value should be provided by seq supplier_order_seq even after changing the primary key?
I have tried referring multiple solutions from the link below but none of them worked.