我有一个 Agency 实体和一个 Role 实体,Agency 实体有一个 READONLY 角色集合。在 Hibernate 5.x 中,以下注释用于对这种关系建模:
@Table(name = "AGENCY", schema = "TEST")
public class Agency {
private Set<Role> roles;
@ManyToMany
@JoinTable(
name = "AGENCY_ROLE",
schema = "TEST",
joinColumns = @JoinColumn(name = "AGENCY_ID"),
inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
@Persister(impl= ReadOnlyCollectionPersister.class)
public Set<Role> getRoles() {
return (roles == null ? null : roles);
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
其中 ReadOnlyCollectionPersister 是自定义类:
public class ReadOnlyCollectionPersister extends BasicCollectionPersister {
private static Collection asInverse(Collection collection) {
collection.setInverse(true);
return collection;
}
public ReadOnlyCollectionPersister(
Collection collectionBinding,
CollectionDataAccess cacheAccessStrategy,
RuntimeModelCreationContext creationContext) throws MappingException,
CacheException {
super(asInverse(collectionBinding), cacheAccessStrategy, creationContext);
}
}
在 Hibernate 6.2 中,@Persister 已弃用,文档中有以下注释 --
*2.4.10。定义自定义实体持久化
历史上允许提供自定义持久性,但从未得到完全支持。 Hibernate 6 提供了更好的替代方法来完成自定义持久器的用例。从 6.2 开始,@Persister 已被正式弃用。*
如果不使用@Persister,我无法找到替代方法来完成我在 Hibernate 6.2 中的用例。有什么见解和/或建议吗?
我搜索并试验了各种方法,但未能找到解决方案。