对于相同的值(REF\u 1\u ID,PROPERTY\u ID)
您可以检索具有相同(REF\u 1\u ID,Property\u ID)的属性实体的数量
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();
propertyId.setIndexValue(indexValue);
您可以使用HibernateInterceptor来实现此功能(onSave方法),在处理此场景时请记住并发性问题
或者
封装如下
@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();
}
}