Crear un proyecto Maven
Escogemos maven-archetype-quickstart
En principio el de org.apache
Añadimos las dependencias de ‘Hibernate’ y ‘Mysql’. Os las pongo aquí.
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <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> </dependencies>
Después dentro de src/main creamos la carpeta ‘resources’: New folder
Dentro de resources creamos el archivo hibernate.cfg.xml, y dentro colocamos lo siguiente:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "https://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/sakila</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property> <mapping class="com.trifulcas.models.Actor" /> </session-factory> </hibernate-configuration>
Creamos un paquete com.trifulcas.models y dentro metemos nuestro POJO:
package com.trifulcas.models; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name= "actor") public class Actor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int actor_id; private String first_name; private String last_name; public Actor() { } /** * @return the actor_id */ public int getActor_id() { return actor_id; } /** * @param actor_id the actor_id to set */ public void setActor_id(int actor_id) { this.actor_id = actor_id; } /** * @return the first_name */ public String getFirst_name() { return first_name; } /** * @param first_name the first_name to set */ public void setFirst_name(String first_name) { this.first_name = first_name; } /** * @return the last_name */ public String getLast_name() { return last_name; } /** * @param last_name the last_name to set */ public void setLast_name(String last_name) { this.last_name = last_name; } @Override public String toString() { return "Actor id=" + actor_id + ",nombre=" + first_name+" "+last_name + "]"; } }
Y por último probaríamos que todo va bien, este código debería funcionar:
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory = meta.getSessionFactoryBuilder().build(); Session session = factory.openSession(); try { Actor penelope = session.get(Actor.class, 1); System.out.println(penelope.getFirst_name() + " " + penelope.getLast_name()); List<Actor> lista = session.createQuery("from Actor a where a.first_name like '%ar%' ").getResultList(); for (Actor a : lista) { System.out.println(a); } } catch (Exception ex) { System.out.println(ex); } factory.close(); session.close();