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

How to persist an enum with JPA?

user8505 Published in September 20, 2018, 12:42 pm

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

share|improve this question
  • Seen from a Java POV, I'd say that an Enum is used to "enumerate" / "list" a bunch of fixed values. For example you could have an enum Letters with the values A, B, C. So if you where to use that in an entity and annotate it with @Enumerated(EnumType.STRING) your variable would be declared like private Letter myLetter and JPA maps the Enum value to VARCHAR (A, B,C). In your program you can continue to profit from the advantages of Enum. So personally I don't see the reason why it needs it's own table. – GameDroids Feb 11 at 15:04
  • 1
    @JsonProperty is nothing to do with the JPA API, so no, no point in that ... for data persistence. – user8558216 Feb 11 at 15:20
  • @DN1, good to know! :P – Dee Feb 11 at 15:29
  • @GameDroids, I agree. And yet I see this as a common implementation when I Google for help... might they know something we don't? Might there be some other logical reason for having it's own table? – Dee Feb 11 at 15:30

2 Answers 2

active oldest votes
up vote 0 down vote
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO