Skip to content

Instantly share code, notes, and snippets.

@rik-degraaff
Created February 12, 2017 12:44
Show Gist options
  • Save rik-degraaff/fdd57adebe7a526d1c88223efc1a31c9 to your computer and use it in GitHub Desktop.
Save rik-degraaff/fdd57adebe7a526d1c88223efc1a31c9 to your computer and use it in GitHub Desktop.
package data;
import javax.persistence.EntityManagerFactory;
import entity.Bild;
import entity.Raum;
import static easyJPA.EasyJPA.*;
import java.util.ArrayList;
import java.util.List;
public class BildDAOImpl implements BildDAO {
private EntityManagerFactory entityManagerFactory = null;
public BildDAOImpl(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
@Override
public boolean insertBild(Bild bild) {
return performTransaction(entityManagerFactory, manager -> {
manager.persist(bild);
return true;
}).orElse(false);
}
@Override
public Raum getRaumByBild(long bildId) {
return performTransaction(entityManagerFactory, manager -> {
Bild bild = manager.find(Bild.class, bildId);
if (bild != null) {
return bild.getRaum();
}
return null;
}).orElse(null);
}
@Override
public List<Raum> getRaumeByBezeichnung(String bezeichnung) {
return performTransaction(entityManagerFactory, manager -> {
return createSelectQuery(manager, Raum.class, (cb, select, r) ->
select.where(cb.equal(r.get("bezeichnung"), bezeichnung))
).getResultList();
}).orElseGet(() -> new ArrayList<>());
}
}
package easyJPA;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
public final class EasyJPA {
@FunctionalInterface
public static interface TriFunction<A,B,C,R> {
R apply(A a, B b, C c);
default <V> TriFunction<A, B, C, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c) -> after.apply(apply(a, b, c));
}
}
public static abstract class Either<L, R> {
Either() {}
public abstract <T> T map(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends T> rightMapper);
public abstract <T> Either<T,R> mapLeft(Function<? super L, ? extends T> mapper);
public abstract <T> Either<L,T> mapRight(Function<? super R, ? extends T> mapper);
public abstract void apply(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer);
public Optional<L> left() {
return map(l -> Optional.ofNullable(l), r -> Optional.empty());
}
public Optional<R> right() {
return map(l -> Optional.empty(), r -> Optional.ofNullable(r));
}
}
public static final class Left<L, R> extends Either<L, R> {
private L value;
public Left(L left) {
value = left;
}
@Override
public <T> T map(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends T> rightMapper) {
return leftMapper.apply(value);
}
@Override
public <T> Either<T, R> mapLeft(Function<? super L, ? extends T> mapper) {
return new Left<>(mapper.apply(value));
}
@Override
public <T> Either<L, T> mapRight(Function<? super R, ? extends T> mapper) {
return new Left<>(value);
}
@Override
public void apply(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer) {
leftConsumer.accept(value);
}
}
public static final class Right<L, R> extends Either<L, R> {
private R value;
public Right(R right) {
value = right;
}
@Override
public <T> T map(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends T> rightMapper) {
return rightMapper.apply(value);
}
@Override
public <T> Either<T, R> mapLeft(Function<? super L, ? extends T> mapper) {
return new Right<>(value);
}
@Override
public <T> Either<L, T> mapRight(Function<? super R, ? extends T> mapper) {
return new Right<>(mapper.apply(value));
}
@Override
public void apply(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer) {
rightConsumer.accept(value);
}
}
private static final class Nothing {}
private EasyJPA() {}
public static <L, R> Either<L, R> performTransaction(EntityManagerFactory factory, Function<EntityManager, ? extends L> statements, Function<PersistenceException, ? extends R> onException) {
Either<L, R> retVal;
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
try {
retVal = new Left<>(statements.apply(manager));
manager.getTransaction().commit();
} catch (PersistenceException e) {
retVal = new Right<>(onException.apply(e));
e.printStackTrace();
if (manager.getTransaction().isActive()) {
manager.getTransaction().rollback();
}
} finally {
manager.close();
}
return retVal;
}
public static <T> Optional<T> performTransaction(EntityManagerFactory factory, Function<EntityManager, T> statements, Consumer<PersistenceException> onException) {
return performTransaction(factory, manager -> {
return statements.apply(manager);
}, e -> {
onException.accept(e);
return new Nothing();
}).left();
}
public static <T> Optional<T> performTransaction(EntityManagerFactory factory, Consumer<EntityManager> statements, Function<PersistenceException, T> onException) {
return performTransaction(factory, manager -> {
statements.accept(manager);
return new Nothing();
}, e -> {
return onException.apply(e);
}).right();
}
public static void performTransaction(EntityManagerFactory factory, Consumer<EntityManager> statements, Consumer<PersistenceException> onException) {
performTransaction(factory, manager -> {
statements.accept(manager);
return new Nothing();
}, e -> {
onException.accept(e);
return new Nothing();
});
}
public static <T> Optional<T> performTransaction(EntityManagerFactory factory, Function<EntityManager, T> statements) {
return performTransaction(factory, statements, e -> {});
}
public static void performTransaction(EntityManagerFactory factory, Consumer<EntityManager> statements) {
performTransaction(factory, statements, e -> {});
}
public static <T, R> TypedQuery<R> createQuery(EntityManager manager, Class<T> cls, BiFunction<CriteriaBuilder, CriteriaQuery<T>, CriteriaQuery<R>> queryMapper) {
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(cls);
return manager.createQuery(queryMapper.apply(cb, cq));
}
public static <T, R> TypedQuery<R> createSelectQuery(EntityManager manager, Class<T> cls, TriFunction<CriteriaBuilder, CriteriaQuery<T>, Root<T>, CriteriaQuery<R>> queryMapper) {
return createQuery(manager, cls, (cb, query) -> {
Root<T> root = query.from(cls);
return queryMapper.apply(cb, query.select(root), root);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment