2012-04-02 10 views
1

私は...以下(セッションsession = HibernateUtil.getSessionFactory.getCurrentSession();)ライン15にNULLポインタを取得しています次のようにHibernateはNULLポインタ

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("[email protected]"); 
    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; 
    } 

} 

誰も標準的なトランザクションの休止状態のユーティリティを持っている場合は、それがエースであることを推薦することができます。しかし、私はnullポインタを取得していますなぜ私が見ることができない、hibernate.cfg.xmlのは、ログは、それはそれはあなたがいないように見えますね

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 

答えて

1

デシベルなどに接続することができました言うように細かい検証しているようですbuildSessionFactory()メソッドで実際にHibernateUtils.sessionFactoryと設定してください。 sessionFactoryは設定されていないので、HibernateUtil.getSessionFactory()に電話するとnullオブジェクトが返されます。あなたはbuildSessionFactory()方法でSessionFactoryのを設定するか、以下のようにコードを変更することができ、次のいずれか

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

ありがとう、私はユーザーに "不明なエンティティ"例外を表示するようですが、私は@Entity任意のアイデアとして注釈を付けましたか? – david99world

+0

問題ありません。あなたの未知のエンティティ例外に対するいくつかの解決策は、[このSOの質問](http://stackoverflow.com/questions/270074/hibernate-jpa-annotations-unknown-entity)にリストされています。 –

1

あなたはHibernateUtilののSessionFactoryを初期化することはありません。 おそらくbuildSessionFactoryは、返すだけでなく、それを設定する必要があります。

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

あなたはセッションファクトリのインスタンスを作成しますが、そのビーイングのヌルので、クラスに割り当てるには参照型 HibernateUtil.sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry); return HibernateUtil.sessionFactory;

代わりの return new Configuration().configure().buildSessionFactory(serviceRegistry); would help.

のデフォルト値を変数

を逃しました