1

So I want to implement a Jpa Repository of my class Reservation.

My RoomType Enum:

public enum RoomType{
    BREUGHELZAAL("Breughel zaal"),
    CARDIJNZAAL("Cardijn zaal"),
    FEESTZAAL("Feest zaal"),
    KEUKEN("Keuken"),
    RECEPTIEZAAL("Receptie zaal"),
    KLEINEKEUKEN("Kleine keuken");

    private String roomType;

    RoomType(String roomType){
       this.roomType= roomType;
    }

    public String getRoomType(){
        return roomType;
    }
}

Now I have no clue how to implement this. I need a List of Enum types in my reservation class, i guess it is something like this, but I don't know the annotation for the enum type:

@OneToMany(cascade = CascadeType.ALL)
List<RoomType> chosenRooms

Thanks in advance for the help!!

5
  • Wouldn't the RoomType be a candidate for making it an entity instead of an enum, anyway? Commented May 23, 2018 at 11:32
  • The roomtypes don't change. There are 6 room types and they never change. So I don't know if making this an entity, is the right solution Commented May 23, 2018 at 11:36
  • Never say never :D. Of course, you know your context best, but an entity is more flexible and probably future-proof. Even when the types don't change, maybe you'd like to store the size or the capacity of a zaal, too. Just brainstorming... Commented May 23, 2018 at 13:04
  • You're right, I could do it like an entity. I'm considering to store the RoomType as a list of entities... I'm not confident that all these annotations do the optimal job :-) Commented May 23, 2018 at 13:07
  • 1
    Btw zaal is dutch for Room xD Commented May 23, 2018 at 13:10

1 Answer 1

7

You don't have sufficient config for Enum persistence, try :

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "RoomType", joinColumns = @JoinColumn(name = "id"))
@Enumerated(EnumType.STRING)
List<RoomType> chosenRooms

@ElementCollection - Defines a collection of instances of a basic type or embeddable class. @CollectionTable - pecifies the table that is used for the mapping of collections of basic or embeddable types (name - name of the collection table, joinColumn - The foreign key columns of the collection table).
Enumerated - Specifies that a persistent property or field should be persisted as a enumerated type.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain what these annotation configure? I would like to know for in the future. I works, but I would like to know why :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.