Hibernate空指针

时间:2012-04-02 16:38:18

标签: java hibernate

我在第15行......下面得到一个空指针(Session session = HibernateUtil.getSessionFactory.getCurrentSession();)

public static void main(String[] args) {
    // TODO Auto-generated method stub
    HibernateUtil.buildSessionFactory();
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    User u = new User();
    u.setEmail("david@hello.co.uk");
    u.setFirstName("David");
    u.setLastName("Gray");

    session.save(u);
    session.getTransaction().commit();
    System.out.println("Record committed");
    session.close();

我的HibernateUtil如下......

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();     
            return new Configuration().configure().buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

偏离主题,但如果任何人也有任何标准事务hibernate utils他们可以推荐那将是ace。但我不知道为什么我得到nullpointer,hibernate.cfg.xml似乎验证正常,因为日志说它可以连接到db等。

INFO : org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
INFO : org.hibernate.Version - HHH000412: Hibernate Core {4.1.0.Final}
INFO : org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
INFO : org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
INFO : org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: /hibernate.cfg.xml
INFO : org.hibernate.cfg.Configuration - HHH000040: Configuration resource: /hibernate.cfg.xml
WARN : org.hibernate.internal.util.xml.DTDEntityResolver - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
INFO : org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: /hibernate.cfg.xml
INFO : org.hibernate.cfg.Configuration - HHH000040: Configuration resource: /hibernate.cfg.xml
WARN : org.hibernate.internal.util.xml.DTDEntityResolver - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!)
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 1
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000006: Autocommit mode: false
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/assessme]
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000046: Connection properties: {user=root, password=****}
INFO : org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
INFO : org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
INFO : org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000232: Schema update complete

3 个答案:

答案 0 :(得分:1)

看起来您实际上并未在HibernateUtils.sessionFactory方法中设置buildSessionFactory()。由于永远不会设置sessionFactory,因此当您调用HibernateUtil.getSessionFactory()时,您将返回一个空对象。您可以在buildSessionFactory()方法中设置sessionFactory,也可以将代码更改为以下内容:

public static void main(String[] args) {
Session session = HibernateUtil.buildSessionFactory().getCurrentSession();

答案 1 :(得分:1)

您永远不会在 HibernateUtil 中初始化 sessionFactory 。 也许 buildSessionFactory 应该设置它而不是仅仅返回,例如

sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry)
return sessionFactory;

答案 2 :(得分:1)

您创建了会话工厂的实例,但未将其分配给类变量

因此它为null,引用类型的默认值     HibernateUtil.sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry); return HibernateUtil.sessionFactory;

而不是    return new Configuration().configure().buildSessionFactory(serviceRegistry); would help.