Jac4e Core

Annotation processor to improve Enums usage in JPA

License

License

GroupId

GroupId

com.github.microtweak
ArtifactId

ArtifactId

jac4e-core
Last Version

Last Version

0.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

Jac4e Core
Annotation processor to improve Enums usage in JPA

Download jac4e-core

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
org.apache.commons : commons-lang3 jar 3.9

provided (1)

Group / Artifact Type Version
javax.persistence : javax.persistence-api jar 2.2

Project Modules

There are no modules declared in this project.

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