我在下面的第15行上得到了一个空指针...(会话会话= 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("[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;
}
}
不在主题之列,但是如果有人也有任何标准的事务休眠实用程序,他们可能会推荐它为ace。但是我看不到为什么我得到了空指针,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
最佳答案
看来您实际上并没有在HibernateUtils.sessionFactory
方法中设置buildSessionFactory()
。由于从未设置sessionFactory
,因此在调用HibernateUtil.getSessionFactory()
时将返回空对象。您可以在buildSessionFactory()
方法中设置sessionFactory,或将代码更改为以下内容:
public static void main(String[] args) {
Session session = HibernateUtil.buildSessionFactory().getCurrentSession();