JPA Descriptor for EclipseLink (RI)

JSON Binding eXtension for JPA

License

License

Categories

Categories

CLI User Interface EclipseLink Data ORM
GroupId

GroupId

com.github.microtweak
ArtifactId

ArtifactId

jbx4j-descriptor-eclipselink
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

JPA Descriptor for EclipseLink (RI)
JSON Binding eXtension for JPA

Download jbx4j-descriptor-eclipselink

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
com.github.microtweak : jbx4j-descriptor-core jar 1.0.0

provided (1)

Group / Artifact Type Version
org.eclipse.persistence : eclipselink jar 2.5.0

test (6)

Group / Artifact Type Version
com.github.microtweak : jbx4j-descriptor-core jar 1.0.0
org.junit.jupiter : junit-jupiter-engine jar 5.3.1
org.junit.jupiter : junit-jupiter-params jar 5.3.1
com.h2database : h2 jar 1.4.197
com.github.dadrus.jpa-unit : jpa-unit5 jar 0.4.0
com.github.dadrus.jpa-unit : jpa-unit-rdbms jar 0.4.0

Project Modules

There are no modules declared in this project.

Jbx4j - JSON Binding eXtension for JPA

A helper library for JPA Entity (de)serialization in JSON.

When we work with JPA and need to (de)serialize JPA entities to JSON (or any other format) we always encounter some problems:

  • Relationship attributes (OneToOne, ManyToOne, OneToMany and ManyToMany) that are Lazy and the JSON Binding framework (like Jackson, GSON, etc) will cause the JPA Provider to make unnecessary queries to the database;
  • Circular reference between JPA mapping;
  • Use of DTOs and Mapping frameworks (like ModelMapper, MapStruct, Dozer, etc.) to bypass the above problems, making development more complex and verbose;
  • Need to create DTOs and Mappers even for simple entities (with two or three attributes).

This library helps in the process of (de)serialization of the JPA entity, reducing the difficulties mentioned above.

Support

Initially we are providing support to the framework below:

Framework Type Minimal version Note
Hibernate JPA 5.2
EclipseLink JPA 2.5 Works only with Weaving enabled. Experimental support
Jackson JSON 2.9.4

Getting Started

  1. You must add a dependency in your project:

    <dependency>
        <groupId>com.github.microtweak</groupId>
        <artifactId>jbx4j-hibernate</artifactId>
        <version>${jbx4j.version}</version>
    </dependency>
    
    <dependency>
        <groupId>com.github.microtweak</groupId>
        <artifactId>jbx4j-jackson</artifactId>
        <version>${jbx4j.version}</version>
    </dependency>
  2. Now let's configure the application to use Jbx4j. Although the following examples are based on the CDI, it is not a requirement to use this library. Create a class that implements EntityResolver as in the example below. You can create multiple EntityResolvers as needed.

    @ApplicationScoped
    public class GenericEntityResolver implements EntityResolver<Object> {
    
        @Inject
        private EntityManager entityManager;
    
        @Override
        public boolean canResolve(Object declaredAt, Class<Object> rawType, JpaEntityData<Object> data, List<Annotation> annotations) {
            return rawType.isAnnotationPresent(Entity.class);
        }
    
        @Override
        public Object resolve(Object declaredAt, Class<Object> rawType, JpaEntityData<Object> data, List<Annotation> annotations) {
            Object primaryKey = data.get("id");
        
            if (primaryKey == null) {
                return null;
            }
        
            // Logic to solve the entity. You can use a find or a complex query JPQL
            return entityManager.find(rawType, primaryKey);
        }    
    
    }
  3. Instantiate the Jbx4j module and add all the EntityResolvers created in the module. After that, register the module in Jackson.

    public class JacksonProducer {
    
        // Ask the CDI to find all the EntityResolver in the application
        @Inject
        private Instance<EntityResolver<?>> resolvers;
        
        @Produces
        @Singleton
        public ObjectMapper createJacksonObjectMapper() {
            ObjectMapper mapper = new ObjectMapper();
            
            // Instances the Jbx4j module for Jackson
            Jbx4jJacksonModule jbx4jModule = new Jbx4jJacksonModule();
    
            // Adds all EntityResolvers found by CDI in the module created earlier
            resolvers.forEach(er -> jbx4jModule.getResolverManager().add(er));
            
            // Register the module in Jackson
            mapper.registerModule(jpaJacksonModule);
            
            return mapper;
        }
        
    }

After these steps, simply ask Jackson's ObjectMapper to (de)serialize your JPA entity as you are already accustomed to doing with other Java classes.

com.github.microtweak

Microtweak

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

Versions

Version
1.0.0