Poni
Poni

Reputation: 11317

Hibernate CriteriaQuery<T> - any straight-forward code example?

I refer to "Chapter 12. Criteria" in this manual.

They nicely explain that:

The javax.persistence.criteria.CriteriaBuilder interface is the first thing with which you need to become acquainted to begin using criteria queries. Its role is that of a factory for all the individual pieces of the criteria. You obtain a javax.persistence.criteria.CriteriaBuilder instance by calling the getCriteriaBuilder method of either javax.persistence.EntityManagerFactory or javax.persistence.EntityManager.

But, none says where/how I get these EntityManager/EntityManagerFactory objects. Sorry, it's just unclear.

I got the feeling it has to do with my Hibernate session, so here's my session code, let me know if/how to change it in order to get an EntityManager object so I can continue, I must be missing something here.

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import x.Debug;

public class HibernateUtil {

    private static final int            DEFAULT_TRANSACTION_TIMEOUT = 3;

    private static final HibernateUtil  m_instance;

    private final SessionFactory        m_session_factory;
    private final ServiceRegistry       m_service_registry;

    private Session                     m_session;

    static {
        m_instance = new HibernateUtil();
    }

    public static HibernateUtil get() {
        return m_instance;
    }

    public HibernateUtil() {
        Configuration configuration = new Configuration();
        configuration.configure();
        this.m_service_registry = new ServiceRegistryBuilder().applySettings(
                configuration.getProperties()).buildServiceRegistry();
        this.m_session_factory = configuration
                .buildSessionFactory(this.m_service_registry);

        this.open_session();
    }

    public void open_session() {
        this.m_session = this.m_session_factory.openSession();
    }

    public void close_session() {
        this.m_session.close();
    }

    public Session get_session() {
        if (!this.m_session.isOpen())
            this.open_session();
        return this.m_session;
    }

    public void save_or_update_objects(final Object[] objects) {
        final Session session = this.get_session();

        try {
            // session.beginTransaction();
            session.getTransaction().setTimeout(DEFAULT_TRANSACTION_TIMEOUT);
            session.getTransaction().begin();

            for (final Object object : objects)
                session.saveOrUpdate(object);

            session.getTransaction().commit();
        } catch (RuntimeException e) {
            session.getTransaction().rollback();
            throw e;
        }
    }

    public Object get_object(final Class<?> clazz, final Serializable id) {
        final Session session = this.get_session();

        if (Debug.DEBUG)
            session.clear();

        final Object ret = session.get(clazz, id);

        return ret;
    }
}

What I did find is how to simply, unrelated to Hibernate, build an EntityManagerFactory and here's the code:

final EntityManagerFactory entity_manager = Persistence.createEntityManagerFactory("persistenceUnitName");

But I'm not sure it's OK, and regarding the "persistenceUnitName" - where did that come from?! Should I just invent a name?

The bottom line is that all I wanted is to search for objects with a certain criteria - Hibernate apparently made it really complicated!

Upvotes: 1

Views: 412

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86754

EntityManager and EntityManagerFactory are interfaces that the provider implements. In the case of Spring, you inject one (See Spring ORM JPA).

In Hibernate, you can use the bootstrapping technique described here. From that reference:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");

Upvotes: 3

Related Questions