hibernate POJO中手动计算字段
我在我的项目中使用 Spring 和 Hibernate。
假设我有一个 USER
表,它通过交叉表与 ROLE
相关,而 ROLE
表又与 PERMISSION
表再次通过交叉表。因此,每个用户可能有多个角色,每个角色可能有多个权限,这些权限可以在不同角色之间共享。在 orm 中,我将有一个像这样的 User 类:
class User {
// blah blah properties
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name="USER_ROLE",
joinColumns={@JoinColumn(name = "USER_ID", nullable = false, updatable = false)},
inverseJoinColumns={@JoinColumn(name = "ROLE_ID", nullable = false, updatable = false)})
private Set<Roles> roles;
}
现在我想要的是在 User
bean 中拥有另一个属性,其中包含用户权限列表作为字符串(List
)。对我来说,执行此操作的最简单方法是,是否可以在填充 User 对象的属性后立即调用该对象的某些方法。该方法将对角色执行一次 for
操作,然后对权限执行另一次操作,并将它们添加到用户对象的权限列表中。
或者也许 hibernate/spring 为这种情况提供了一些东西?
聚苯乙烯 我尝试用谷歌搜索这个但没有成功。
I'm using Spring coupled with Hibernate in my project.
Suppose I've got a USER
table which is related to ROLE
through a cross-table and ROLE
table in turn is related to PERMISSION
table again through a cross-table. So each user may have several roles and each role may have several permissions which may be shared between different roles. In the orm i will have a User class like this:
class User {
// blah blah properties
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name="USER_ROLE",
joinColumns={@JoinColumn(name = "USER_ID", nullable = false, updatable = false)},
inverseJoinColumns={@JoinColumn(name = "ROLE_ID", nullable = false, updatable = false)})
private Set<Roles> roles;
}
now what i want is to have another property in the User
bean containing a list of permissions of the user as Strings (List<String> permissions
). the easiest way to do this for me would be if it was possible to call some method of the User object right after it's properties have been populated. The method will do a for
over the roles and then another one over the permissions and add them to the list of permissions in the user object.
Or maybe hibernate/spring provide something for this exact case?
P.S.
I tried Googling this but was unsuccessful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 @PostLoad 回调 将帮助你解决这个问题。它允许您在加载实体后立即进行计算或修改,以便您可以构建权限列表。
I think a @PostLoad callback will help you with this. It allows you to do calculations or modifications right after the entity has been loaded so you can build up the permissions list then.