在我看来:这种情况表明所有者和名称不再是自然ID。它们只是属性,从服务级别可以不时更改,并且owner, name
从业务级别的角度来看,具有唯一值对是有限制的。因此,“除非我说的是不可变的”情况应该在“业务”服务级别解决:即所有者变为可变的,并且在您的服务内部创建单独的方法来修改它,其他方法不应修改此属性。IE:
@Secured(ADMIN_ONLY_ACCESS)
public SomeEntity modifyAdministrative(SomeEntity entity) {
//here you allow to modify owner
}
public SomeEntity modify(SomeEntity entity) {
// here owner should be used as readonly property
// and you can controll this only on your own,
// hibernate is not able to support you anymore with immutability control
}
或者您可以将某些实体表的数据映射到第二个不同的实体,并重NaturalId
用hibernate提供的行为,即
public class SomeEntity {
// Keep this for general usage
@Id private Integer id;
@NaturalId private User owner;
@NaturalId(mutable=true) private String name;
...
}
public class SomeEntityForAdmin {
// use this for administrative usage
@Id private Integer id;
private User owner;
private String name;
...
}
SomeEntityForAdmin
仅用于管理情况,当需要更改owner
. 所有其余代码都保持原样。
但请注意:您将遇到复杂的缓存问题(您必须为缓存创建适当的失效策略,一旦发生更改SomeEntityForAdmin
或发生),并且SomeEntity
在涉及同一事务时会变得一团糟。SomeEntity
SomeEntityForAdmin