Hibernate version: Hibernate ORM 4.2.1
Here's the scenario. There are two fields in object "Product".
1) "id" is the product id and it is a oracle sequence.
2) "userGeneratedProductId" is user generated product id, if user populates it which creating the "Product" object, it should be persisted with that value. If user doesn't populate it, it should be the same value as product id "id". (At the time of persisting the product object we don't have id populated yet, and yes, we don't want to update the object with the field set after retrieving the inserted product)
How do we achieve this, does hibernate provide a way to have a field which is replica of an id filed if its not populated in the object. Does it provide interceptor calls after getting the next value on the sequence and before generating the insert statement.
Below is the sniplet of variables defined in the "Product" class.
Code:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_sequence")
@Column(name = "PRODUCT_ID")
protected Long id;
Code:
@Column(name = "USER_GENERATED_PRODUCT_ID")
protected String userGeneratedProductId;
Here's is how we persist the prduct object.
Code:
em.persist(product);