For the same values of (REF_1_ID, PROPERTY_ID)
You can retrieve how many Property Entity with the same (REF_1_ID, PROPERTY_ID) you have
Integer indexValue = (Integer)
session.createQuery("select count(*) from Property p where p.propertyId.rederenceId = :referenceId and p.propertyId.propertyId = :propertyId")
.setParameter("referenceId", referenceId)
.setParameter("propertyId", propertyId)
.iterate()
.next();
Then you set up
propertyId.setIndexValue(indexValue);
You can use an HibernateInterceptor to achieve this funcionality (onSave method) Keep in mind concurrency issues when dealing with this scenario
Or encapsulate as follows
@IdClass(PropertyId.class)
public class Property {
private Integer referenceId;
private Integer propertyId;
private Integer indexValue;
/**
* You can use some repository instead
*
* setUp by using Either constructor Or setter injection
*/
private Session session;
public Property() {}
public Property(Session session) {
this.session = session;
}
@Id
public Integer getIndexValue() {
if(indexValue != null)
return indexValue;
return (Integer)
session.createQuery("select count(*) from Property p where p.propertyId.rederenceId = :referenceId and p.propertyId.propertyId = :propertyId")
.setParameter("referenceId", referenceId)
.setParameter("propertyId", propertyId)
.iterate()
.next();
}
}