包含映射资源时,SessionFactory为null

时间:2013-12-29 21:11:15

标签: java hibernate

运行Program类的main方法时出现以下错误:

  

线程“main”java.lang.NullPointerException中的异常       在Program.main(Program.java:9)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:606)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)   创建会话工厂的问题   处理以退出代码1完成

我认为这是我设置映射的问题,因为当我从配置文件中删除以下内容时没有任何问题:

<mapping resource="User.hbm.xml" />

这是我的配置文件(hibernate.cfg.xml)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/protein_tracker</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">**********</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="User.hbm.xml" />
    </session-factory>
</hibernate-configuration>

这是我的映射文件(User.hbm.xml):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="User" table="users">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="total" type="int">
            <column name="total" />
        </property>
        <property name="goal" type="int">
            <column name="goal" />
        </property>
    </class>
</hibernate-mapping>

这是我的主要课程:

import org.hibernate.Session;

public class Program {

    public static void main(String[] args) {

        System.out.println("hello");

        Session session = HibernateUtils.getSessionFactory().openSession();

        session.beginTransaction();

        User user = new User();
        user.setName("John");
        user.setTotal(0);
        user.setGoal(250);
        session.save(user);


        session.getTransaction().commit();

       session.close();
       HibernateUtils.getSessionFactory().close();

    }

}

这是HibernateUtils类:

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

    private static SessionFactory sessionFactory;
    private static StandardServiceRegistryBuilder builder;

    static {
        try {
            Configuration configuration = new Configuration().configure();
            builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (HibernateException e) {
            System.out.println("problem creating session factory");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

我刚刚开始学习hibernate,所以我不确定问题是什么。任何帮助,将不胜感激。谢谢!

0 个答案:

没有答案