package no.valg.valgnatt.database.dao;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import no.valg.valgnatt.database.hibernate.Tilstand;
import no.valg.valgnatt.database.hibernate.Tilstand_;

import org.hibernate.Session;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.stereotype.Repository;

@Repository
public class TilstandDao extends BaseDao<Tilstand, Integer> {
    public Tilstand findByNokkel(String nokkel) {
        Session session = sessionFactory.getCurrentSession();
        CriteriaBuilder cb = session.getCriteriaBuilder();
        CriteriaQuery<Tilstand> cq = cb.createQuery(Tilstand.class);
        Root<Tilstand> root = cq.from(Tilstand.class);
        cq.select(root)
                .where(cb.equal(root.get(Tilstand_.nokkel), nokkel));
        return session.createQuery(cq).getSingleResult();
    }
}
