Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map EnumSet (or List of Enums) in an entity using JPA2

I have entity Person:

@Entity
@Table(schema="", name="PERSON")
public class Person {
    List<PaymentType> paymentTypesList;
    //some other fields     
    //getters and setters and other logic
}

and I have enum PaymentType:

public enum PaymentType {
    FIXED, CO_FINANCED, DETERMINED;
}

How to persist Person and its list of enums (in this list I have to place variable amount of enums, there may be one of them, or two or all of them)

I'm using Spring with Postgres, Entity are created using JPA annotation, and managed using Hibernate.

like image 301
arteq Avatar asked Nov 14 '22 11:11

arteq


1 Answers

Ask yourself, wheter PaymentTypes can change over time.

I would create a @Entity PaymentType with one name attribute, and create a @Many2Many between PaymentType and Person.

Another approach: @ElementCollection See ElementCollection from From Wikibooks, the open-content textbooks collection

like image 156
pihentagy Avatar answered Mar 13 '23 07:03

pihentagy