Home How to persist an enum with JPA?
Reply: 2

How to persist an enum with JPA?

Dee
1#
Dee Published in 2018-02-11 14:39:44Z

I'm using MySQL and JPA.

I have an enum that has it's own table.

I have a field in an entity (entity1) that uses this enum. This field is annotated with: @Enumeration(EnumType.STRING).

1 - is it correct to persist this field in entity1 as a column in the db when it has it's own table?

2 - if I am using @JsonProperty on my other fields and answer to 1 is "yes", must I use @JsonProperty on the enum field too?

3 - what's the point in having the enum in a separate table?

Currently, with just the @Enumeration annotation and a column for the enum for entity1 I get error: was annotated as enumerated, but its java type is not an enum

osama yaccoub
2#
osama yaccoub Reply to 2018-02-11 16:13:10Z

1- How I addressed similar problem was as follows :

I defined the enum in a separate entity :

@Entity
@Table(name="CALC_METHOD")
public class CalculationMethod {


public CalculationMethod() {
    super();
    // TODO Auto-generated constructor stub
}


@Id
@Enumerated(EnumType.STRING)
@Column(name="METHOD_NAME")
private CalculationMethodId calcMethodID;

@Column(name="DISPLAY_TEXT")
private String displayName;
.
.
.
.

then I refered to it in another entity as follows :

@OneToOne
@JoinColumn(name="CALCULATION_METHOD",referencedColumnName="METHOD_NAME")
private CalculationMethod calculationMethod;

that way it's stored in a seprate table, yet referenced from another entity with no duplication ... the point here is that you can't map enum scoped variables so when I needed to store a display name for the enum value, I needed to make it a separate attribute as you see

3- why to store it in table? because from the java POV it was really an enum , and I want to apply some calculation methods polymorpically (like calculate some value in the refering entity using the calculation method, so I defined calculate() method for each calculation method , each with a different implementation then call it while calculating a whole) the I wanted it to be always read with the same value and display name from many places in the code , and If I want to modify the display name, it's done only @ one place -thus consistency and maintainability-

2- it depends on the requirement and your json model

UHDante
3#
UHDante Reply to 2018-02-12 08:44:59Z

For your situation I normaly use an entity on BBDD for the ENUM like:

AuthenticationType
id, name, value : (0, CERT, Certificate)

Where name is the real ENUM and value is the text I want to represent on the views.

For that you need the following:

public enum AuthenticationTypeEnum{
   CERT, PASS;
}

@Entity
@Table(name = "AuthenticationType")
public class AuthenticationType{
    @Column(name = "ID")
    private Long id;
    @Column(name = "NAME")
    @Enumerated(EnumType.STRING)
    private AuthenticationTypeEnum name; // REAL ENUM TYPE
    @Column(name = "VALUE")
    private String value;
    ....
}

@Entity...
class Authentication{
    private String login;
    ...
    @ManyToOne
    private AuthenticationType type; // ENUM USE
    ...
}

In that way you can edit the value of your ENUM on BBDD without changing your code, for me this is one of the best options. Hope this helps.

When you persist the entity use the cascade all on JPA to persist also the enum entity.

NOTE: On normal situations, the enums not change, so you set them only ones. They are a prerequisite to the application so they change on rare circumstances.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.320845 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO