1.- Crear proyecto Maven (maven-archetype-quickstart)
2.- Añadir dependencias: Mysql e hibernate
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.32</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.6.15.Final</version> </dependency>
3.- Creo la clase HibernateUtil
package com.trifulcas.hibernate; import java.util.Properties; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { try { Configuration configuration = new Configuration(); // Hibernate settings equivalent to hibernate.cfg.xml's properties Properties settings = new Properties(); settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver"); settings.put(Environment.URL, "jdbc:mysql://localhost:3306/sakila"); settings.put(Environment.USER, "root"); settings.put(Environment.PASS, ""); settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect"); settings.put(Environment.SHOW_SQL, "true"); settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread"); configuration.setProperties(settings); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { e.printStackTrace(); } } return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } }
4.- Pongo los datos correctos de conexión: BD, usuario y password (settings.put(Environment.URL, “jdbc:mysql://localhost:3306/biblioteca”);)
5.- Creo mi entidad
package com.trifulcas.models; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "genero") public class Genero { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "idgenero") private int idGenero; private String nombre; public Genero() { super(); // TODO Auto-generated constructor stub } public int getIdGenero() { return idGenero; } public void setIdGenero(int idGenero) { this.idGenero = idGenero; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } @Override public String toString() { return "Genero [idGenero=" + idGenero + ", nombre=" + nombre + "]"; } }
6.- Lo anotamos en hibernateutil
... configuration.addAnnotatedClass(Genero.class); ...
7.- Lo pruebo:
Session session = HibernateUtil.getSessionFactory().openSession(); try { Genero g = session.get(Genero.class, 1); System.out.println(g); } catch (Exception ex) { System.out.println(ex); } session.close();