Diferencia entre revisiones de «Usuario:Lmorillas/desarrollo web servidor/jee/intro hibernate»

De WikiEducator
Saltar a: navegación, buscar
(Página creada con '{{MiTitulo| Introducción a Hibernate}} {{Actividad| == Objetivos == * Introducción a Hibernate * Utilizar Hibernate en nuestra aplicación == Tareas == # Concepto de Framewor…')
 
(hibernate.cfg.xml)
 
(8 revisiones intermedias por el mismo usuario no mostrado)
Línea 1: Línea 1:
 
{{MiTitulo| Introducción a Hibernate}}
 
{{MiTitulo| Introducción a Hibernate}}
 +
 +
__NOTOC__
  
 
{{Actividad|
 
{{Actividad|
Línea 20: Línea 22:
 
== Guión ==
 
== Guión ==
 
* Arquitectura java, págs. 152-179
 
* Arquitectura java, págs. 152-179
 +
{{Tip|
 +
== Más docs.==
 +
* http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html_single/
 +
* http://www.cursohibernate.es/doku.php
 
}}
 
}}
 +
}}
 +
 +
Ejemplo de http://www.cursohibernate.es/lib/exe/fetch.php?media=unidades:unidad_02.zip
 +
 +
== hibernate.cfg.xml ==
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://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/hibernate1</property>
 +
    <property name="connection.username">hibernate1</property>
 +
    <property name="connection.password">hibernate1</property>
 +
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
 +
    <property name="hibernate.show_sql">true</property>
 +
 +
 +
    <mapping resource="ejemplo01/Profesor.hbm.xml"/>
 +
 +
  </session-factory>
 +
</hibernate-configuration>
 +
</source>
 +
 +
==Profesor.java ==
 +
<source lang="java">
 +
package ejemplo01;
 +
 +
import java.io.Serializable;
 +
 +
 +
 +
public class Profesor implements Serializable  {
 +
 +
    private int id;
 +
    private String nombre;
 +
    private String ape1;
 +
    private String ape2; 
 +
 +
    public Profesor(){
 +
    }
 +
 +
    public Profesor(int id, String nombre, String ape1, String ape2) {
 +
        this.id = id;
 +
        this.nombre = nombre;
 +
        this.ape1 = ape1;
 +
        this.ape2 = ape2;
 +
    }
 +
   
 +
    // getters y setters ...
 +
}
 +
</source>
 +
 +
== Profesor.hbm.xml ==
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 +
<hibernate-mapping>
 +
  <class name="ejemplo01.Profesor" table="Profesor" >
 +
    <id column="Id" name="id" type="integer"/>
 +
    <property name="nombre" />
 +
    <property name="ape1" />
 +
    <property name="ape2" />
 +
  </class>
 +
</hibernate-mapping>
 +
</source>
 +
 +
 +
== Main.java ==
 +
<source lang="java">
 +
package ejemplo01;
 +
 +
import org.hibernate.Session;
 +
import org.hibernate.SessionFactory;
 +
import org.hibernate.cfg.Configuration;
 +
import org.hibernate.service.ServiceRegistry;
 +
import org.hibernate.service.ServiceRegistryBuilder;
 +
 +
 +
public class Main {
 +
 +
    public static void main(String[] args) {
 +
        SessionFactory sessionFactory;
 +
 +
        Configuration configuration = new Configuration();
 +
        configuration.configure();
 +
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
 +
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
 +
       
 +
        Profesor profesor=new Profesor(101, "Juan", "Perez", "García");
 +
       
 +
        Session session=sessionFactory.openSession();
 +
       
 +
        session.beginTransaction();
 +
        session.save(profesor);
 +
        session.getTransaction().commit();
 +
       
 +
       
 +
        Profesor profesor2=(Profesor)session.get(Profesor.class,101);
 +
        System.out.println(profesor2.getId());
 +
        System.out.println(profesor2.getNombre());
 +
        System.out.println(profesor2.getApe1());
 +
        System.out.println(profesor2.getApe2());       
 +
       
 +
        profesor2.setNombre("Daniel");
 +
       
 +
        session.beginTransaction();
 +
        session.update(profesor2);
 +
        session.getTransaction().commit();       
 +
 +
        session.beginTransaction();
 +
        session.delete(profesor2);
 +
        session.getTransaction().commit();
 +
       
 +
       
 +
        session.close();
 +
        sessionFactory.close();
 +
    }
 +
}
 +
</source>

Última revisión de 01:03 1 feb 2013




Icon activity.jpg

Actividad

Objetivos

  • Introducción a Hibernate
  • Utilizar Hibernate en nuestra aplicación

Tareas

  1. Concepto de Framework de persistencia.
  2. Instalación de Hibernate.
  3. Introducción a Hibernate
  4. Configuración de Hibernate
  5. Insertar objetos en la base de datos con Hibernate.
  6. Selección de objetos de la base de datos con Hibernate.
  7. Seleccionar un único objeto con Hibernate
  8. Borrar objetos de la base de datos con Hibernate.
  9. Filtrar objetos de la base de datos con Hibernate
  10. Construcción de la clase Hibernate Helper.
  11. Mapeo del fichero de configuración.
  12. El principio de Convención sobre configuración

Guión

  • Arquitectura java, págs. 152-179
Icon present.gif






Ejemplo de http://www.cursohibernate.es/lib/exe/fetch.php?media=unidades:unidad_02.zip

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://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/hibernate1</property>
    <property name="connection.username">hibernate1</property>
    <property name="connection.password">hibernate1</property>
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.show_sql">true</property>
 
 
    <mapping resource="ejemplo01/Profesor.hbm.xml"/>
 
  </session-factory>
</hibernate-configuration>

Profesor.java

package ejemplo01;
 
import java.io.Serializable;
 
 
 
public class Profesor implements Serializable  {
 
    private int id;
    private String nombre;
    private String ape1;
    private String ape2;   
 
    public Profesor(){ 
    }
 
    public Profesor(int id, String nombre, String ape1, String ape2) {
        this.id = id;
        this.nombre = nombre;
        this.ape1 = ape1;
        this.ape2 = ape2;
    }
 
    // getters y setters ...
}

Profesor.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="ejemplo01.Profesor" table="Profesor" >
    <id column="Id" name="id" type="integer"/>
    <property name="nombre" />
    <property name="ape1" />
    <property name="ape2" />
  </class>
</hibernate-mapping>


Main.java

package ejemplo01;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
 
 
public class Main {
 
    public static void main(String[] args) {
        SessionFactory sessionFactory;
 
        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
 
        Profesor profesor=new Profesor(101, "Juan", "Perez", "García");
 
        Session session=sessionFactory.openSession();
 
        session.beginTransaction();
        session.save(profesor);
        session.getTransaction().commit();
 
 
        Profesor profesor2=(Profesor)session.get(Profesor.class,101);
        System.out.println(profesor2.getId());
        System.out.println(profesor2.getNombre());
        System.out.println(profesor2.getApe1());
        System.out.println(profesor2.getApe2());        
 
        profesor2.setNombre("Daniel");
 
        session.beginTransaction();
        session.update(profesor2);
        session.getTransaction().commit();        
 
        session.beginTransaction();
        session.delete(profesor2);
        session.getTransaction().commit(); 
 
 
        session.close();
        sessionFactory.close();
    }
}