Yervand Aghababyan
Yervand Aghababyan

Reputation: 1140

Manually calculated field in hibernate POJO

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.

Upvotes: 1

Views: 1114

Answers (1)

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11607

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.

Upvotes: 2

Related Questions