=====Ouverture d'une session=====
Jusqu'à la version 4.2 :
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
La version 4.3 :
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
À partir de la version 5 :
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
=====import annotations=====
Utilisez la version générique de JPA
import javax.persistence;
plutôt que celle spécifique à Hibernate.
import org.hibernate.annotations;
=====createCriteria=====
Jusqu'à la version 5.0 :
import org.hibernate.Criteria;
Criteria criteria = session.createCriteria(Pays.class);
List retour = criteria.list();
À partir de la version 5.2 :
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(Pays.class);
List retour = session.createQuery(criteria).getResultList();
=====createQuery=====
Jusqu'à la version 5.0 :
import org.hibernate.Query;
Query query = session.createQuery("from Pays");
return query.list();
À partir de la version 5.2 :
import org.hibernate.query.Query;
Query query = session.createQuery("from Pays", Pays.class);
return query.getResultList();