如果我有两个实体:
@Entity
@Table(name="PARENT_ENTITY")
public class ParentEntity {
@Id
@Column(name="ID")
private Integer id;
// ... Other fields
@Column(name="COLUMN_2")
private String column2;
// ... Other fields
// Getters and setters
}
@Entity
@Table(name="CHILD_ENTITY")
public class ChildEntity {
@Id
@Column(name="ID")
private Integer id;
@Column(name="PARENT_ID")
private Integer parentId;
// ... Other fields
@Formula("(select COLUMN_2 from {schema}.PARENT_ENTITY p where p.ID = PARENT_ID)")
private String column2;
// Getters and setters
}
当我持久化childentity的示例时,@formula属性不会被填充:
session.saveOrUpdate(childEntity)
childEntity.getColumn2(); // this equals ""
childEntity.postValidate() // validation that relies on @Formula values
// I want to persist if validation succeeds, but not persist if validation fails
session.flush()
有没有一种有效的方法可以做到这一点,而不必每次都去查询父实体?
1条答案
按热度按时间5us2dqdw1#
您可以通过执行查询,例如。
entityManager.createNativeQuery
或者使用entityManager.refresh
刷新实体。