Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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">
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>5.2.10.Final</version>
- </dependency>
- <hibernate-configuration>
- <session-factory>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url"> jdbc:mysql://localhost:3306/base_prueba</property>
- <property name="connection.username">root</property>
- <property name="connection.password">tomazon-33</property>
- <property name="connection.pool_size">1</property>
- <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
- <property name="show_sql">true</property>
- <property name="hbm2ddl.auto">create-drop</property>
- <mapping class="mapeo/Contacto.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- package Main;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.transaction.HeuristicMixedException;
- import javax.transaction.RollbackException;
- import org.hibernate.HibernateException;
- public class Main
- {
- public static void main(String[] args) throws HibernateException
- {
- try {
- ContactosDAO contactosDAO = new ContactosDAO();
- Contacto contactoRecuperado = null;
- long idAEliminar = 0;
- Contacto contacto1 = new Contacto("Contacto 1", "contacto1@contacto.com", "12345678");
- Contacto contacto2 = new Contacto("Contacto 2", "contacto2@contacto.com", "87654321");
- Contacto contacto3 = new Contacto("Contacto 3", "contacto3@contacto.com", "45612378");
- idAEliminar = contactosDAO.guardaContacto(contacto1);
- contactosDAO.guardaContacto(contacto2);
- contactosDAO.guardaContacto(contacto3);
- contacto2.setNombre("Nuevo Contacto 2");
- contactosDAO.actualizaContacto(contacto2);
- contactoRecuperado = contactosDAO.obtenContacto(idAEliminar);
- System.out.println("Recuperamos a " + contactoRecuperado.getNombre());
- contactosDAO.eliminaContacto(contactoRecuperado);
- List<Contacto> listaContactos = contactosDAO.obtenListaContactos();
- System.out.println("Hay " + listaContactos.size() + "contactos en la base de datos");
- for(Contacto c : listaContactos)
- {
- System.out.println("-> " + c.getNombre());
- }
- } catch (RollbackException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- } catch (HeuristicMixedException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- package Main;
- import java.io.Serializable;
- 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="contactos")
- public class Contacto implements Serializable
- {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private long id;
- private String nombre;
- @Column(name="e_mail")
- private String email;
- private String telefono;
- public Contacto()
- {
- }
- public Contacto(String nombre, String email, String telefono)
- {
- this.nombre = nombre;
- this.email = email;
- this.telefono = telefono;
- }
- public String getEmail()
- {
- return email;
- }
- public void setEmail(String email)
- {
- this.email = email;
- }
- public long getId()
- {
- return id;
- }
- private void setId(long id)
- {
- this.id = id;
- }
- public String getNombre()
- {
- return nombre;
- }
- public void setNombre(String nombre)
- {
- this.nombre = nombre;
- }
- public String getTelefono()
- {
- return telefono;
- }
- public void setTelefono(String telefono)
- {
- this.telefono = telefono;
- }
- }
- public class ContactosDAO
- {
- private Session sesion;
- private Transaction tx;
- public long guardaContacto(Contacto contacto) throws HibernateException, RollbackException, HeuristicMixedException
- {
- long id = 0;
- try
- {
- iniciaOperacion();
- id = (Long) sesion.save(contacto);
- tx.commit();
- } catch (HibernateException he)
- {
- try {
- manejaExcepcion(he);
- throw he;
- } catch (IllegalStateException | SystemException ex) {
- Logger.getLogger(ContactosDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (HeuristicRollbackException | SecurityException | SystemException ex) {
- Logger.getLogger(ContactosDAO.class.getName()).log(Level.SEVERE, null, ex);
- } finally
- {
- sesion.close();
- }
- return id;
- }
- package hibernatenormal;
- import org.hibernate.HibernateException;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtil
- {
- private static final SessionFactory sessionFactory;
- static
- {
- try
- {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- } catch (HibernateException he)
- {
- System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
- throw new ExceptionInInitializerError(he);
- }
- }
- public static SessionFactory getSessionFactory()
- {
- return sessionFactory;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement