Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="hibernate.connection.url">jdbc:mysql://localhost/pruebahibernate</property>
  9. <property name="hibernate.connection.username">root</property>
  10. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  11. <property name="hibernate.search.autoregister_listeners">false</property>
  12. <property name="hibernate.show_sql">true</property>
  13. <mapping resource="src/entidades/Contacto.hbm.xml"/>
  14. </session-factory>
  15. </hibernate-configuration>
  16.  
  17. public class HibernateUtil
  18. {
  19. private static final SessionFactory sessionFactory;
  20.  
  21. static
  22. {
  23. try
  24. {
  25. sessionFactory = new Configuration().configure().buildSessionFactory();
  26. } catch (HibernateException he)
  27. {
  28. System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
  29. throw new ExceptionInInitializerError(he);
  30. }
  31. }
  32.  
  33. public static SessionFactory getSessionFactory()
  34. {
  35. return sessionFactory;
  36. }
  37. }
  38.  
  39. public class ContactoDAO
  40. {
  41. private Session sesion;
  42. private Transaction tx;
  43.  
  44. public long guardaContacto(Contacto contacto) throws HibernateException
  45. {
  46. long id = 0;
  47.  
  48. try
  49. {
  50. iniciaOperacion();
  51. id = (Long) sesion.save(contacto);
  52. tx.commit();
  53. } catch (HibernateException he)
  54. {
  55. manejaExcepcion(he);
  56. throw he;
  57. } finally
  58. {
  59. sesion.close();
  60. }
  61.  
  62. return id;
  63. }
  64.  
  65. private void iniciaOperacion() throws HibernateException
  66. {
  67. sesion = HibernateUtil.getSessionFactory().openSession();
  68. tx = sesion.beginTransaction();
  69. }
  70. }
  71.  
  72. <?xml version="1.0"?>
  73. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  74. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  75. <!-- Generated 30/01/2017 23:44:31 by Hibernate Tools 3.5.0.Final -->
  76. <hibernate-mapping>
  77. <class name="entidades.Contacto" table="CONTACTO">
  78. <id name="id" type="long">
  79. <column name="ID" />
  80. <generator class="assigned" />
  81. </id>
  82. <property name="nombre" type="java.lang.String">
  83. <column name="NOMBRE" />
  84. </property>
  85. <property name="email" type="java.lang.String">
  86. <column name="EMAIL" />
  87. </property>
  88. <property name="telefono" type="java.lang.String">
  89. <column name="TELEFONO" />
  90. </property>
  91. </class>
  92. </hibernate-mapping>
  93.  
  94. Exception in thread "main" java.lang.ExceptionInInitializerError
  95. at entidades.ContactoDAO.iniciaOperacion(ContactoDAO.java:102)
  96. at entidades.ContactoDAO.guardaContacto(ContactoDAO.java:20)
  97. at ejemplo.hibernate.PruebaHibernate.main(PruebaHibernate.java:23)
  98. Caused by: java.lang.NullPointerException
  99. at org.hibernate.cfg.Configuration.addResource(Configuration.java:560)
  100. at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
  101. at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
  102. at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
  103. at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
  104. at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
  105. at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
  106. at entidades.HibernateUtil.<clinit>(HibernateUtil.java:17)
  107.  
  108. private Transaction iniciaOperacion() throws HibernateException {
  109. sesion = HibernateUtil.getSessionFactory().openSession();
  110. tx = sesion.beginTransaction();
  111. return tx
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement