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

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

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

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

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.function.Supplier;

public class JpaQueryCall, T> extends JpaCall, List> {

    private Class entityClass;

    private String query;

    public JpaQueryCall(Supplier managerSupplier, Class entityClass, String query) {
        super(managerSupplier);
        this.entityClass = entityClass;
        this.query = query;
    }

    public JpaQueryCall(EntityManager manager, Class entityClass, String query) {
        this(() -> manager, entityClass, query);
    }

    @Override
    public List call(List params) throws Exception {
        Query queryObject = getManager().createQuery(query);
        for(QueryParameter param : params) {
            queryObject.setParameter(param.getName(), param.getValue());
        }
        return queryObject.getResultList();
    }
}