Skip to content

Annotation processor to improve Enums usage in JPA

License

Notifications You must be signed in to change notification settings

microtweak/jac4e

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jac4e - Jpa Attribute Converter for Enum

Annotation processor to improve Enums usage in JPA.

Problem

JPA 2.1 introduced the AttributeConverter interface allowing the use of custom types in JPA entities. This feature brought an alternative to the well-known @Enumerated.

However, implementing a new AttributeConverter for each enum is a bit annoying and tiring. Even implementing a generic converter, the problem still continues.

Solution

Introduced an annotation processor that generates the implementation of the AttributeConverter for each enum annotated with @EnumAttributeConverter. This implementation uses a property declared in the enum to do the conversion.

Usage

  1. Setup your pom.xml
<project>
    <properties>
        <jac4e.version>0.0.2</jac4e.version>
    </properties>

    <dependencies>
        <!-- Other dependencies -->

        <dependency>
            <groupId>com.github.microtweak</groupId>
            <artifactId>jac4e-core</artifactId>
            <version>${jac4e.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Other plugins -->

            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <dependencies>
                    <dependency>
                        <groupId>com.github.microtweak</groupId>
                        <artifactId>jac4e-processor</artifactId>
                        <version>${jac4e.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.github.microtweak.jac4e.processor.Jac4eProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. Annotate your enum and create the "value" property. The type and content of this property will be persisted in the database.
@EnumAttributeConverter
public enum Status {

    PENDING('P'),

    SHIPPED('S'),

    COMPLETED('C');

    private char value; // Pay attention: the APT uses it for conversion
    
    Status(char value) {
        this.value = value;
    }
}
  1. Declare the enum in the entity as a common property
@Entity
public class Order {

    @Id
    private Long id;

    private Payment paymentMode; // Now you should NOT use @Enumerated

    private double total;

    // Getters and Setters

}