Hibernate sessionfactory nullpointerexception

时间:2014-07-10 11:29:59

标签: java mysql hibernate nullpointerexception sessionfactory

我正在练习休眠。我正在尝试连接本地mysql服务器并获取一些信息,但在我的控制台上有一些错误消息。我调试了我的代码,并在我尝试创建会话工厂时找到了初始部分

     Configuration configuration = new Configuration();

这部分代码导致java.lang.NullPointerException错误。我很确定我已经导入了所有的hibernate jar。

hibernate.cfg.xml中:

    <?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'>
    <hibernate-configuration>

<session-factory>

<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_query_cache">true</property>


<property name='dialect'>org.hibernate.dialect.MySQLDialect</property>
<property name='connection.driver_class'>com.mysql.jdbc.Driver</property>
<property name='connection.url'>jdbc:mysql://localhost:3306/hotel</property>
<property name='connection.username'>root</property>
<property name='connection.password'>root123</property>

<property name='cache.provider_class'>org.hibernate.cache.HashtableCacheProvider</property>

<property name='transaction.factory_class'>org.hibernate.transaction.JDBCTransactionFactory</property>
<property name='current_session_context_class'>thread</property>
<property name='hibernate.show_sql'>false</property>

 <!-- List of XML mapping files -->
   <mapping resource='Hotel.hbm.xml'/>
   <mapping resource='Car_Rental.hbm.xml'/>
   <mapping resource='Hotel_Reservation.xml'/>
   <mapping resource='Ticket_Reservation.hbm.xml'/>
   <mapping resource='Tour_Reservation.hbm.xml'/>
   <mapping resource='Touristic_Place.hbm.xml'/>
   <mapping resource='User.hbm.xml'/>

   </session-factory>

</hibernate-configuration>

这是我正在尝试构建sessionfactory的地方:

    public static SessionFactory createSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure();
        //Configuration configuration = new Configuration().configure(new             File("C:\\Users\\user\\workspace\\Hotel\\src\\hibernate.cfg.xml"));
        serviceRegistry = new ServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

控制台:

    javax/transaction/SystemException
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at screens.Screen_User.save(Screen_User.java:211)
    at screens.Screen_User$1.actionPerformed(Screen_User.java:182)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)

我正在为此工作2天,但我无法弄明白。任何帮助都会很棒。谢谢。

0 个答案:

没有答案