构建sessionFactory对象hibernate核心时是否有空指针异常?

时间:2015-06-21 08:35:38

标签: hibernate

Hibernate核心xml + java

这里我尝试构建会话工厂对象,但获取NullPointerException。

CHibernateUtil.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CHibernateUtil {
    static SessionFactory factory;
    static{
        try{
            Configuration cfg = new Configuration();
            cfg = cfg.configure();
            System.out.println(cfg);
            //org.hibernate.cfg.Configuration@b3f9b8
            factory = cfg.buildSessionFactory(); // Here i am getting nullpointer exception.
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static SessionFactory getSessionFactory(){
        return factory;
    }
}

的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="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/dao_example
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            rajesh
        </property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="show_sql">
            true
        </property>
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/india/hibernate/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

执行客户端代码后:

java.lang.NullPointerException
    at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:87)
    at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:148)
    at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:41)
    at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:104)
    at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:398)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:199)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1043)
    at com.jlcindia.util.CHibernateUtil.<clinit>(CHibernateUtil.java:12)
    at com.jlcindia.util.CHibernateTemplate.saveObject(CHibernateTemplate.java:11)
    at com.jlcindia.dao.HibernateCustomerDAO.addCustomer(HibernateCustomerDAO.java:12)
    at com.jlcindia.dao.Lab1.main(Lab1.java:10)
java.lang.NullPointerException
    at com.jlcindia.util.CHibernateTemplate.saveObject(CHibernateTemplate.java:12)
    at com.jlcindia.dao.HibernateCustomerDAO.addCustomer(HibernateCustomerDAO.java:12)
    at com.jlcindia.dao.Lab1.main(Lab1.java:10)
Exception in thread "main" java.lang.NullPointerException
    at com.jlcindia.dao.HibernateCustomerDAO.addCustomer(HibernateCustomerDAO.java:12)
    at com.jlcindia.dao.Lab1.main(Lab1.java:10)

我尝试了这个,我仍然得到NullPointerException:

        Configuration cfg = new Configuration();
        cfg = cfg.configure("hibernate.cfg.xml");
        factory = cfg.buildSessionFactory();

我不明白为什么会出现这种异常是否有任何配置错误?请帮我弄清楚这个。

0 个答案:

没有答案