I started by wanting to get rid of an ERole enum type that my TA used to enumerate some roles that users can have. It looks like this:
public enum ERole {
CUSTOMER,
MANAGER,
ADMIN
}
Because we’re learning spring boot, the way that my TA persisted these Roles in a database was like this:
@Entity
@RequiredArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Role {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Integer id;
@Enumerated( EnumType.STRING )
@Column( length = 20 )
private ERole name;
}
The “role” table is needed because a user can have multiple roles. A many-to-many relationship between the “user” and “role” tables is used.
@Entity
@Table ( uniqueConstraints = { @UniqueConstraint ( columnNames = { "username", "email" } ) } )
@RequiredArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class User {
@Id
@GeneratedValue ( strategy = GenerationType.IDENTITY )
private Long id;
@Column ( nullable = false, length = 20 )
private String username;
@Column ( nullable = false, length = 50 )
private String email;
@Column ( nullable = false, length = 120 )
private String password;
@ManyToMany ( fetch = FetchType.LAZY )
@JoinTable ( name = "user_roles",
joinColumns = @JoinColumn ( name = "user_id" ),
inverseJoinColumns = @JoinColumn ( name = "role_id" ) )
private Set <Role> roles = new HashSet <> ( );
}
My goals are the following:
- Have just one Role enum/class where I can define my Role types
- Have a “role” table be automatically created
- Have these Role types be automatically inserted into the “role” table with an automatically generated and unique ID
- Be able to create a JpaRepository for my Role enum/class
So far I’ve managed to come up with the following:
@Entity
public enum MyRole {
CUSTOMER,
MANAGER,
ADMIN;
@Id
@GeneratedValue ( strategy = GenerationType.IDENTITY )
private Integer id;
}
and MyRoleRepository, which looks like this:
public interface MyRoleRepository extends JpaRepository <MyRole, Integer> {
}
But I am getting this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRoleRepository' defined in org.example.springskeleton.user.MyRoleRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Unable to make field private final java.lang.String java.lang.Enum.name accessible: module java.base does not "opens java.lang" to unnamed module @4988d8b8
From what I’ve searched through documentation (@Entity, @Target, ElementType), I understand that @Entity can be used with enum types, according to its @Target annotation, but so far I have not found any examples of how that is intended to be done.
In essence, I think I have two questions:
- Is there a better way of doing what my TA was trying to do?
- If so, is my idea of having an enum Entity a better way/even possible?
This is my first SO question, so I’m sorry if I’ve done anything incorrectly. Thanks for the help!
Dragos Mercean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.