问题描述
我在下面的第15行上获得了一个空指针...(会话会话= HibernateUtil.getSessionFactory.getCurrentSession();)
I'm getting a null pointer on line 15...below (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("[email protected]");
u.setFirstName("David");
u.setLastName("Gray");
session.save(u);
session.getTransaction().commit();
System.out.println("Record committed");
session.close();
我的HibernateUtil如下...
My HibernateUtil is as follows...
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.但是我看不到为什么要得到nullpointer,hibernate.cfg.xml似乎可以通过日志证明它可以连接到db等,因此可以很好地验证.
Off topic, but if anyone also has any standard transaction hibernate utils they could recommend that would be ace. But I can't see why I'm getting the nullpointer, the hibernate.cfg.xml seems to validate fine as the logs say it could connect to the db etc.
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
推荐答案
似乎您实际上没有在 buildSessionFactory()
方法中设置 HibernateUtils.sessionFactory
.由于从未设置 sessionFactory
,因此在调用 HibernateUtil.getSessionFactory()
时将返回空对象.您可以在 buildSessionFactory()
方法中设置sessionFactory,或将代码更改为以下内容:
It looks like you're not actually setting HibernateUtils.sessionFactory
in your buildSessionFactory()
method. Since sessionFactory
is never set, you're returning a null object when you call HibernateUtil.getSessionFactory()
. You can either set sessionFactory in your buildSessionFactory()
method, or change your code to the following:
public static void main(String[] args) {
Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
这篇关于休眠空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!