![]() Let’s create a custom convert between our sample enum and String so that we can remove enum values without causing an exception when fetching records already stored in a database but simply map them to null. Implementing a converter requires writing two methods for mapping between these types in both directions. ![]() It has two generic type parameters which indicate input and output types for the converter. ![]() The attribute convert is defined as a Java interface. Therefore, the knowledge you will get here has a wider application. Converters give you the option to implement your custom enum mapping strategy.Īctually, you can use attribute converters with any Java type, not only enums. JPA attribute converters go one step further. Generally speaking, the annotation allows you to choose if JPA should map the values of an enum using its ordinal() or name() method. If JPA attribute converters aren’t the best way to map Java enums, they’re certainly the most flexible. Best way to map enum – JPA attribute converts If you think it’s a small cost for better fault tolerance, save enum as strings. The main drawback is higher memory consumption. What is more, records are also easier to read directly in a database. You also don’t have to add new values at the end of the list. Pretty straightforward, isn’t it? Enum as string – pros and consīecause EnumType.STRING uses the name() method of the enum, changing the order of values in the enum doesn’t affect mappings for records already saved in a database. Using ORDINAL acts as the default JPA behavior. As you probably suspect, STRING indicates that JPA should save enum values as text. This enum has two values: STRING and ORDINAL. The annotation accepts EnumType as a parameter. You only need to inform JPA about your intention in an entity which holds the enum using annotation called Let’s see an Task Difficulty difficulty JPA allows you to store enum values as text. Otherwise, we would use IP addresses instead of domain names, right? Saving enum as string with JPAĬomputers prefer numbers for enumerations but humans understand the text better. ![]() Fortunately, it’s very easy to override the default approach. Summing up, because of potential bugs which default JPA enum mapping may cause, I recommend to avoid it. The reader needs to know the order of values in the enum to understand what is the meaning of individual numbers. You can of course update all records in your database every time you do a change to the enum but it’s not very practical.Īdditionally, readability of enum values directly in a database is poor. From now, JPA will map all 0 values stored in a database to this new enum value even though you’ve never saved it. Imagine you added a new value called LOWEST as a first value in the Difficulty enum. Otherwise, values which you stored in a database will be mapped to different values when you try reading them. You always must add new enum values at the end of the list. However, there is one huge disadvantage in default JPA behavior when you plan to add more values to your enum. If you care about memory consumption, this might be your preferred approach. The main benefit of this approach is low use of memory. There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and. In our sample enum, JPA will map LOW value to 0, NORMAL to 1, and HIGH to 2. These numbers are associated with the order in which you define values in the enum. What it means is that without customizations JPA stores enum value as numbers. By default, when an enum is a part of an entity, JPA maps its values into numbers using the ordinal() method. But for the demonstration purpose, we’re going to use a simple enum presented below.įirst of all, in order to save enum values in a relational database using JPA, you don’t have to do anything. Since you found this article, you probably already have your own enum which you plan to store in a database. I’ll compare these methods and present the best application for them. From this post, you’ll learn different methods for saving Java enums in a relational database using JPA. * Java program to demonstrate how to convert Enum to String in Java.Saving enum values in a relational database is a bit more tricky than primitives, Strings, and regular immutable value wrappers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |