Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. package hibernateanotaciones;
  2.  
  3. public Contacto()
  4. {
  5. }
  6.  
  7. public Contacto(String nombre, String email, String telefono)
  8. {
  9. this.nombre = nombre;
  10. this.email = email;
  11. this.telefono = telefono;
  12. }
  13.  
  14. public String getEmail()
  15. {
  16. return email;
  17. }
  18.  
  19. public void setEmail(String email)
  20. {
  21. this.email = email;
  22. }
  23.  
  24. public long getId()
  25. {
  26. return id;
  27. }
  28.  
  29. private void setId(long id)
  30. {
  31. this.id = id;
  32. }
  33.  
  34. public String getNombre()
  35. {
  36. return nombre;
  37. }
  38.  
  39. public void setNombre(String nombre)
  40. {
  41. this.nombre = nombre;
  42. }
  43.  
  44. public String getTelefono()
  45. {
  46. return telefono;
  47. }
  48.  
  49. public void setTelefono(String telefono)
  50. {
  51. this.telefono = telefono;
  52. }
  53.  
  54. package hibernateanotaciones;
  55.  
  56. static
  57. {
  58. try
  59. {
  60. sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
  61. } catch (HibernateException he)
  62. {
  63. System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
  64. throw new ExceptionInInitializerError(he);
  65. }
  66. }
  67.  
  68. public static SessionFactory getSessionFactory()
  69. {
  70. return sessionFactory;
  71. }
  72.  
  73. package persistencia;
  74.  
  75. public static void iniciaOperacion() throws HibernateException
  76.  
  77. package persistencia;
  78. import hibernateanotaciones.Contacto;
  79. import java.util.List;
  80. import org.hibernate.HibernateException;
  81. public class ContactoDAO {
  82.  
  83. public long guardarContacto(Contacto contacto)throws HibernateException
  84. {
  85. long id=0;
  86. try
  87. {
  88. Conexion.iniciaOperacion();
  89. id=(Long)Conexion.sesion.save(contacto);
  90. Conexion.tx.commit();
  91. }catch(HibernateException he)
  92. {
  93. Conexion.manejaExcepcion(he);
  94. throw he;
  95. }
  96. finally
  97. {
  98. Conexion.sesion.close();
  99. }
  100. return id;
  101. }
  102. public void actualizarContacto(Contacto contacto) throws HibernateException
  103. {
  104. try
  105. {
  106. Conexion.iniciaOperacion();
  107. Conexion.sesion.update(contacto);
  108. Conexion.tx.commit();
  109. }catch(HibernateException he)
  110. {
  111. Conexion.manejaExcepcion(he);
  112. }
  113. finally
  114. {
  115. Conexion.sesion.close();
  116. }
  117. }
  118. public void eliminarContacto(Contacto contacto) throws HibernateException
  119. {
  120. try
  121. {
  122. Conexion.iniciaOperacion();
  123. Conexion.sesion.delete(contacto);
  124. Conexion.tx.commit();
  125. }catch(HibernateException he)
  126. {
  127. Conexion.manejaExcepcion(he);
  128. }
  129. finally
  130. {
  131. Conexion.sesion.close();
  132. }
  133. }
  134. public Contacto obtenerContacto(long idContacto) throws HibernateException
  135. {
  136. Contacto contacto=null;
  137. try
  138. {
  139. Conexion.iniciaOperacion();
  140. contacto=(Contacto)Conexion.sesion.get(Contacto.class, idContacto);
  141. Conexion.tx.commit();
  142. }catch(HibernateException he)
  143. {
  144. Conexion.manejaExcepcion(he);
  145. }
  146. finally
  147. {
  148. Conexion.sesion.close();
  149. }
  150. return contacto;
  151. }
  152. public List<Contacto> listarContactos() throws HibernateException
  153. {
  154. List<Contacto> listarContactos=null;
  155. try
  156. {
  157. Conexion.iniciaOperacion();
  158. listarContactos=Conexion.sesion.createQuery("from Contacto").list();
  159. Conexion.tx.commit();
  160. }catch(HibernateException he)
  161. {
  162. Conexion.manejaExcepcion(he);
  163. }
  164. finally
  165. {
  166. Conexion.sesion.close();
  167. }
  168. return listarContactos;
  169. }
  170.  
  171. import java.util.List;
  172.  
  173. /**
  174. * @param args the command line arguments
  175. */
  176. public static void main(String[] args) {
  177. long idConsultar=0;
  178. Contacto contactoRecuperado=null;
  179.  
  180. Contacto contacto1=new Contacto("Maria", "maria@gmail.com", "02122344332");
  181. Contacto contacto2=new Contacto("Paco", "paco@gmail.com", "02121221332");
  182. Contacto contacto3=new Contacto("Luis", "luis@gmail.com", "02124432111");
  183.  
  184. ContactoDAO contactoDAO=new ContactoDAO();
  185. idConsultar=contactoDAO.guardarContacto(contacto1);
  186. contactoDAO.guardarContacto(contacto2);
  187. contactoDAO.guardarContacto(contacto3);
  188.  
  189. contacto2.setNombre("Petrolina");
  190. contactoDAO.actualizarContacto(contacto2);
  191.  
  192. contactoRecuperado=contactoDAO.obtenerContacto(idConsultar);
  193. System.out.println("El contacto "+contactoRecuperado.getNombre()+" Se ha recuperado");
  194.  
  195. contactoDAO.eliminarContacto(contactoRecuperado);
  196.  
  197. List<Contacto> listarContacto=contactoDAO.listarContactos();
  198. System.out.println("Hay "+listarContacto.size()+" Contactos en la base de datos");
  199.  
  200. for(Contacto c:listarContacto)
  201. {
  202. System.out.println("-> "+c.getNombre());
  203. }
  204.  
  205.  
  206.  
  207. }
  208.  
  209. INFO: HHH000206: hibernate.properties not found
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement