Why and how can @Entity be used with enum types?

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:

  1. Have just one Role enum/class where I can define my Role types
  2. Have a “role” table be automatically created
  3. Have these Role types be automatically inserted into the “role” table with an automatically generated and unique ID
  4. 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:

  1. Is there a better way of doing what my TA was trying to do?
  2. 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!

New contributor

Dragos Mercean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật