package at.fhj.swd14.pse.repository;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

public abstract class AbstractRepository<T> {

    protected final Class<T> entityClass;

    @PersistenceContext(unitName = "SEP")
    protected EntityManager entityManager;

    protected AbstractRepository(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public T find(Long id) {
        return entityManager.find(entityClass, id);
    }

    public void save(T t) {
        entityManager.persist(t);
    }

    public List<T> findAll() {
        TypedQuery<T> query = entityManager.createQuery(
                "SELECT entity FROM " + entityClass.getTypeName() + " entity" + " ORDER BY entity.id", entityClass);

        return query.getResultList();
    }

    public void refresh(T t) {
        entityManager.refresh(t);
    }

    public void remove(T t) {
        entityManager.remove(t);
    }

    public List<T> executeNamedQuery(String name) {
        TypedQuery<T> query = entityManager.createNamedQuery(name, entityClass);
        return query.getResultList();
    }

    public List<T> executeNamedQuery(String name, Map<String, Object> parameter) {
        TypedQuery<T> query = entityManager.createNamedQuery(name, entityClass);
        for (String key : parameter.keySet()) {
            query.setParameter(key, parameter.get(key));
        }
        return query.getResultList();
    }

    public int executeNativeQuery(String query) {
        Query nativeQuery = entityManager.createNativeQuery(query);
        return nativeQuery.executeUpdate();
    }
}

@@ -1294,17 +1294,38 @@
 or (
-String ke
+Map.Entry%3CString, Object%3E entr
 y : 
@@ -1334,18 +1334,20 @@
 rameter.
-k
 e
+ntr
 ySet()) 
@@ -1383,30 +1383,39 @@
 ter(
-key, parameter.get(key
+entry.getKey(), entry.getValue(
 ));%0A

use entrySet instead of keySet