Diferencia entre revisiones de «Usuario:Lmorillas/desarrollo web servidor/jee/intro hibernate»
De WikiEducator
(→BuildSessionFactory) |
(→hibernate.cfg.xml) |
||
(4 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 23: | Línea 25: | ||
== Más docs.== | == Más docs.== | ||
* http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html_single/ | * 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"> | <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.ServiceRegistry; | ||
import org.hibernate.service.ServiceRegistryBuilder; | import org.hibernate.service.ServiceRegistryBuilder; | ||
− | |||
− | |||
− | |||
− | |||
− | + | public class Main { | |
− | + | public static void main(String[] args) { | |
− | + | SessionFactory sessionFactory; | |
− | ServiceRegistry serviceRegistry = new ServiceRegistryBuilder(). | + | 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> | </source> |
Última revisión de 02:03 1 feb 2013
Objetivos
Tareas
Guión
Tip:
Más docs.
|
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(); } }