All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datafx.crud.jpa.JpaCrudService Maven / Gradle / Ivy

There is a newer version: 8.0.1
Show newest version
package io.datafx.crud.jpa;

import io.datafx.crud.BasicCrudService;
import io.datafx.crud.util.EntityWithId;

import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.function.Supplier;

public class JpaCrudService, T extends Serializable> extends BasicCrudService {

    private CreateOnceSupplier entityManagerSupplier;

    private EntityManager createdManager;

    public JpaCrudService(EntityManager entityManager, Class entityClass) {
        this(() -> entityManager, entityClass);
    }

    public JpaCrudService(Supplier entityManagerSupplier, Class entityClass) {
       this(new CreateOnceSupplier(entityManagerSupplier), entityClass);
    }

    public JpaCrudService(CreateOnceSupplier entityManagerSupplier, Class entityClass) {
        super(new JpaGetAllCall(entityManagerSupplier, entityClass),
                new JpaGetByIdCall(entityManagerSupplier, entityClass),
                new JpaDeleteCall(entityManagerSupplier, entityClass),
                new JpaPersistCall(entityManagerSupplier),
                new JpaUpdateCall(entityManagerSupplier));
        this.entityManagerSupplier = entityManagerSupplier;
    }

    public EntityManager getEntityManager() {
        if(createdManager == null) {
            createdManager = entityManagerSupplier.get();
        }
        return createdManager;
    }
}