Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <dependency>
  7. <groupId>org.hibernate</groupId>
  8. <artifactId>hibernate-core</artifactId>
  9. <version>5.2.10.Final</version>
  10. </dependency>
  11.  
  12. <hibernate-configuration>
  13.  
  14. <session-factory>
  15. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  16. <property name="connection.url"> jdbc:mysql://localhost:3306/base_prueba</property>
  17. <property name="connection.username">root</property>
  18. <property name="connection.password">tomazon-33</property>
  19.  
  20. <property name="connection.pool_size">1</property>
  21.  
  22. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  23.  
  24. <property name="show_sql">true</property>
  25. <property name="hbm2ddl.auto">create-drop</property>
  26.  
  27.  
  28.  
  29. <mapping class="mapeo/Contacto.hbm.xml"/>
  30. </session-factory>
  31. </hibernate-configuration>
  32.  
  33. package Main;
  34.  
  35. import java.util.List;
  36. import java.util.logging.Level;
  37. import java.util.logging.Logger;
  38. import javax.transaction.HeuristicMixedException;
  39. import javax.transaction.RollbackException;
  40. import org.hibernate.HibernateException;
  41.  
  42. public class Main
  43. {
  44. public static void main(String[] args) throws HibernateException
  45. {
  46. try {
  47. ContactosDAO contactosDAO = new ContactosDAO();
  48. Contacto contactoRecuperado = null;
  49. long idAEliminar = 0;
  50.  
  51. Contacto contacto1 = new Contacto("Contacto 1", "contacto1@contacto.com", "12345678");
  52. Contacto contacto2 = new Contacto("Contacto 2", "contacto2@contacto.com", "87654321");
  53. Contacto contacto3 = new Contacto("Contacto 3", "contacto3@contacto.com", "45612378");
  54.  
  55.  
  56. idAEliminar = contactosDAO.guardaContacto(contacto1);
  57. contactosDAO.guardaContacto(contacto2);
  58. contactosDAO.guardaContacto(contacto3);
  59.  
  60. contacto2.setNombre("Nuevo Contacto 2");
  61. contactosDAO.actualizaContacto(contacto2);
  62.  
  63. contactoRecuperado = contactosDAO.obtenContacto(idAEliminar);
  64. System.out.println("Recuperamos a " + contactoRecuperado.getNombre());
  65.  
  66. contactosDAO.eliminaContacto(contactoRecuperado);
  67.  
  68.  
  69. List<Contacto> listaContactos = contactosDAO.obtenListaContactos();
  70. System.out.println("Hay " + listaContactos.size() + "contactos en la base de datos");
  71.  
  72. for(Contacto c : listaContactos)
  73. {
  74. System.out.println("-> " + c.getNombre());
  75. }
  76. } catch (RollbackException ex) {
  77. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  78. } catch (HeuristicMixedException ex) {
  79. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  80. }
  81. }
  82. }
  83.  
  84. package Main;
  85.  
  86. import java.io.Serializable;
  87. import javax.persistence.Column;
  88. import javax.persistence.Entity;
  89. import javax.persistence.GeneratedValue;
  90. import javax.persistence.GenerationType;
  91. import javax.persistence.Id;
  92. import javax.persistence.Table;
  93.  
  94. @Entity
  95. @Table(name="contactos")
  96. public class Contacto implements Serializable
  97. {
  98. @Id
  99. @GeneratedValue(strategy=GenerationType.IDENTITY)
  100. private long id;
  101. private String nombre;
  102.  
  103. @Column(name="e_mail")
  104. private String email;
  105. private String telefono;
  106.  
  107. public Contacto()
  108. {
  109. }
  110.  
  111. public Contacto(String nombre, String email, String telefono)
  112. {
  113. this.nombre = nombre;
  114. this.email = email;
  115. this.telefono = telefono;
  116. }
  117.  
  118. public String getEmail()
  119. {
  120. return email;
  121. }
  122.  
  123. public void setEmail(String email)
  124. {
  125. this.email = email;
  126. }
  127.  
  128. public long getId()
  129. {
  130. return id;
  131. }
  132.  
  133. private void setId(long id)
  134. {
  135. this.id = id;
  136. }
  137.  
  138. public String getNombre()
  139. {
  140. return nombre;
  141. }
  142.  
  143. public void setNombre(String nombre)
  144. {
  145. this.nombre = nombre;
  146. }
  147.  
  148. public String getTelefono()
  149. {
  150. return telefono;
  151. }
  152.  
  153. public void setTelefono(String telefono)
  154. {
  155. this.telefono = telefono;
  156. }
  157. }
  158.  
  159. public class ContactosDAO
  160. {
  161. private Session sesion;
  162. private Transaction tx;
  163.  
  164. public long guardaContacto(Contacto contacto) throws HibernateException, RollbackException, HeuristicMixedException
  165. {
  166. long id = 0;
  167.  
  168. try
  169. {
  170. iniciaOperacion();
  171. id = (Long) sesion.save(contacto);
  172. tx.commit();
  173. } catch (HibernateException he)
  174. {
  175. try {
  176. manejaExcepcion(he);
  177. throw he;
  178. } catch (IllegalStateException | SystemException ex) {
  179. Logger.getLogger(ContactosDAO.class.getName()).log(Level.SEVERE, null, ex);
  180. }
  181. } catch (HeuristicRollbackException | SecurityException | SystemException ex) {
  182. Logger.getLogger(ContactosDAO.class.getName()).log(Level.SEVERE, null, ex);
  183. } finally
  184. {
  185. sesion.close();
  186. }
  187.  
  188. return id;
  189. }
  190.  
  191. package hibernatenormal;
  192.  
  193. import org.hibernate.HibernateException;
  194. import org.hibernate.SessionFactory;
  195. import org.hibernate.cfg.Configuration;
  196.  
  197. public class HibernateUtil
  198. {
  199. private static final SessionFactory sessionFactory;
  200.  
  201. static
  202. {
  203. try
  204. {
  205. sessionFactory = new Configuration().configure().buildSessionFactory();
  206. } catch (HibernateException he)
  207. {
  208. System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
  209. throw new ExceptionInInitializerError(he);
  210. }
  211. }
  212.  
  213. public static SessionFactory getSessionFactory()
  214. {
  215. return sessionFactory;
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement