Jac4e

Annotation processor to improve Enums usage in JPA

License

License

GroupId

GroupId

com.github.microtweak
ArtifactId

ArtifactId

jac4e
Last Version

Last Version

0.0.5
Release Date

Release Date

Type

Type

pom
Description

Description

Jac4e
Annotation processor to improve Enums usage in JPA
Project URL

Project URL

https://github.com/microtweak/jac4e
Source Code Management

Source Code Management

https://github.com/microtweak/jac4e

Download jac4e

Filename Size
jac4e-0.0.5.pom 4 KB
Browse

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.microtweak/jac4e/ -->
<dependency>
    <groupId>com.github.microtweak</groupId>
    <artifactId>jac4e</artifactId>
    <version>0.0.5</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.microtweak/jac4e/
implementation 'com.github.microtweak:jac4e:0.0.5'
// https://jarcasting.com/artifacts/com.github.microtweak/jac4e/
implementation ("com.github.microtweak:jac4e:0.0.5")
'com.github.microtweak:jac4e:pom:0.0.5'
<dependency org="com.github.microtweak" name="jac4e" rev="0.0.5">
  <artifact name="jac4e" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.github.microtweak', module='jac4e', version='0.0.5')
)
libraryDependencies += "com.github.microtweak" % "jac4e" % "0.0.5"
[com.github.microtweak/jac4e "0.0.5"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

  • core
  • processor
  • test

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

}
com.github.microtweak

Microtweak

Small libraries for micro tweaking (or small features) in frameworks you already know

Versions

Version
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1