sessionfactory hibernate中的java.lang.NullPointerException

时间:2017-08-03 18:22:14

标签: java hibernate orm nullpointerexception sessionfactory

我正在尝试使用hibernate构建一个应用程序。问题是,sessionFactory无法打开会话,因为Session session = sessionFactory.openSession();处存在NullPointerException,这也是myAccount.addElement("August", 50000, 14000, 10000, 10000, 16000);的原因。

主要课程

package myaccount1;
import myaccount.entity1.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.List;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;

public class MyAccount {


private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}

public static void main(String[] args) {

        MyAccount myAccount = new MyAccount();

        System.out.println("Adding an element");

        myAccount.addElement("August", 50000, 14000, 10000, 10000, 16000);
        myAccount.addElement("September", 50000, 14000, 10000, 10000, 16000);

        System.out.println("List of elements");



    }

public void addElement(String mounth, int salary, int must_save_for_rent, int must_save_for_education, int must_save, int balance) {
    Session session = sessionFactory.openSession();
    Transaction transaction = null;

    try {
        transaction = session.beginTransaction();
        MyAccountEntity element = new MyAccountEntity(mounth, salary, must_save_for_rent, must_save_for_education, must_save, balance);
        session.save(element);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
            e.printStackTrace();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }

}
}

配置文件

<?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/app?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">sopromat</property>
<mapping class="myaccount.entity1.MyAccountEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>

HibernateUtil类

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


public class HibernateUtil {
    private static SessionFactory sessionFactory = 
            buildSessionFactory();
    private static ServiceRegistry serviceRegistry;

    private static SessionFactory buildSessionFactory() {
        try {

            Configuration configuration = new Configuration();
            configuration.configure();
            //Use config file path explicitly 
            //configuration.configure("hibernate/hibernate.cfg.xml");
            serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
            sessionFactory = configuration
                    .buildSessionFactory(serviceRegistry);
            return sessionFactory;

        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory "
                    + "creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}

映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="myaccount.entity1.MyAccountEntity" table="myaccount">
      <id name="id" type="int" column="id">
            <generator class="auto_increment"/>
        </id>
        <property name="mounth" column="mounth" type="string"/>
        <property name="salary" column="salary" type="int"/>
        <property name="must_save_for_rent" column="must_save_for_rent" type="int"/>
        <property name="must_save_for_education" column="must_save_for_education" type="int"/>
        <property name="must_save" column="must_save" type="int"/>
        <property name="balance" column="balance" type="int"/>
    </class>
</hibernate-mapping>

项目结构 enter image description here

怎么了? 请帮忙,我不知道该怎么办。

0 个答案:

没有答案