Configuración por XML

Contactar con el profesor

Esta manera es bastante más engorrosa que utilizando anotaciones, y por tanto, la desaconsejo.

Los XML

./src/hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.isolation">2</property>
		<property name="hibernate.connection.password"></property>
		<property name="hibernate.connection.pool_size">10</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/prueba_hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.current_session_context_class">managed</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>

		<mapping resource="resources/autor.hbm.xml" />
		<mapping resource="resources/libro.hbm.xml" />

	</session-factory>
</hibernate-configuration>

./src/resources/autor.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"
http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
">

<hibernate-mapping>

	<class name="com.hibernate.entities.Autor">
		<meta attribute="class-description">
			This class contains the autor detail.
		</meta>

		<id name="id" type="int" column="id">
			<generator class="native" />
		</id>

		<property name="nombre" column="titulo" type="string" />

		<set name="libros" table="LIBRO" inverse="true" lazy="true"
			fetch="select" cascade="all">
			<key>
				<column name="autor_id" not-null="true" />
			</key>
			<one-to-many class="com.hibernate.entities.Libro" />
		</set>
	</class>

</hibernate-mapping>

./src/resources/libro.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"
http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
">

<hibernate-mapping>

	<class name="com.hibernate.entities.Libro" table="LIBRO">

		<meta attribute="class-description">
			This class contains the libro detail.
		</meta>

		<id name="id" type="int" column="id">
			<generator class="native" />
		</id>

		<property name="titulo" column="titulo" type="string" />
		<property name="precio" type="int" />
		<many-to-one name="Autor"
			class="com.hibernate.entities.Autor" column="autor_id" 
			not-null="false" cascade="all" />
	</class>

</hibernate-mapping>

Las entidades Java

./com/hibernate/entities/Autor.java

package com.hibernate.entities;

import java.util.Set;

public class Autor {
	private int id;
	private String nombre;
	private Set<Libro> libros;
	
	... getters y setters
	
}

./src/com/hibernate/entities

package com.hibernate.entities;

public class Libro {

	private int id;
	private String titulo;
	private int precio;
	private Autor autor;

	... getters y setters
}

HibernateUtil

package com.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {
	private static StandardServiceRegistry registry;
	private static SessionFactory sessionFactory;

	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			try {
				registry = new StandardServiceRegistryBuilder().configure().build();
				MetadataSources sources = new MetadataSources(registry);
				Metadata metadata = sources.getMetadataBuilder().build();
				sessionFactory = metadata.getSessionFactoryBuilder().build();
			} catch (Exception e) {
				e.printStackTrace();
				if (registry != null) {
					StandardServiceRegistryBuilder.destroy(registry);
				}
			}
		}
		return sessionFactory;
	}
}
← Configuración por anotaciones
Probando la configuración con una transacción →

Aviso Legal | Política de privacidad