S
S
Skoleev2021-08-20 14:00:25
Java
Skoleev, 2021-08-20 14:00:25

What is the best annotation to use for roles (for Hibernate) in Spring?

Here, for example, I have

@Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.EAGER)
    private Set<Role> roles;r>

It created one label USER_ROLES
Where, when creating each user, it re-writes its role with text and specifies the id.
Is this approach good in terms of optimization? Or is there another, better one?
For example, two tables and one connecting them, so that the same values ​​\u200b\u200bare not constantly repeated, but simply the role id is indicated.

I will be glad to answer, I really want to start writing good applications from the very beginning.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-08-20
@Skoleev

Good afternoon.
I usually do it the other way.
1) I create enum UserRole, UserPrivilege
2) I create entity Role. If necessary, you can also add a set of Privilege for each role
3) Further, each entity contains a role or a set of roles.
4) I connect the User entity with the UserRole entity
Here are the sources:
The User entity

@Entity
@Data
@Table(name = "users")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class User {

  //...

  @JsonBackReference
  @ToString.Exclude
  @ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(
      name = "users_roles",
      joinColumns = @JoinColumn(name = "user_id"),
      inverseJoinColumns = @JoinColumn(name = "role_id"))
  private Set<Role> roles;

}

Role entity
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@Table(name = "roles")
public class Role implements GrantedAuthority {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  private Long roleId;

  @JsonBackReference
  @ToString.Exclude
  @ManyToMany(mappedBy = "roles")
  private Collection<User> users;

  @JsonBackReference
  @ToString.Exclude
  @ManyToMany
  @JoinTable(
      name = "roles_privileges",
      joinColumns = @JoinColumn(name = "role_id"),
      inverseJoinColumns = @JoinColumn(name = "privilege_id"))
  private Collection<Privilege> privileges;

  @Enumerated(EnumType.STRING)
  private UserRole name;

  public Role() {
    super();
  }

  public Role(UserRole name) {
    super();
    this.name = name;
  }

  @Override
  public String getAuthority() {
    return name.name();
  }
}

enum UserRole
public enum UserRole {
  USER,
  ADMIN
}

essence Privilege
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@Table(name = "privileges")
public class Privilege {

  

  @Enumerated(EnumType.STRING)
  private UserPrivilege name;

  @JsonBackReference
  @ToString.Exclude
  @ManyToMany(mappedBy = "privileges")
  private Collection<Role> roles;


}

enum UserPrivilege
public enum UserPrivilege {
  ADMIN_PRIVILEGE,
  READ_PRIVILEGE,
  WRITING_COMMENTS_PRIVILEGE
}

M
Mikhail Osher, 2021-08-20
@miraage

I'm generally zero in Java, but when I dabbled with Spring, the articles on this site helped a lot.
https://www.baeldung.com/role-and-privilege-for-sp...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question