I've been looking through discussions on natural v surrogate key. I'm not asking about that. I'm using both.
However, I need to generate the natural id, not assign it. The surrogate key is already auto-generated and only one field can be auto-generated. I would like to initialize the natural id with the value of the surrogate key.
So, for annotation:
Code:
public class customer {
...
@Id
@GeneratedValue
private Integer id;
@Column(name="customer_id")
private Integer customerId;
..
}
I am currently doing this for every time I create a new entity:
Code:
Customer c = new Customer();
em.persist(c);
c.setCustomerId(c.getId());
em.persist(c);
I'm not so concerned about the performance. I'm concerned I may introduce a bug by forgetting to init the natural id of some of my entities. Here's hoping there is a underside API to hibernate that lets me omit the two last lines above.
Thanks,
Regards,
John