gpt4 book ai didi

java - 无法创建 sessionFactory 对象.java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 14:08:09 27 4
gpt4 key购买 nike

我正在写申请。该应用程序使用 hibernate 来访问数据库。转到代码:我有这样的代码:

package logic;

import org.hibernate.Session;

public class Main {
public static void main(String[] args){
Prepod prepod = new Prepod();
Student student = new Student();
prepod.getStudents().add(student);
student.getPrepods().add(prepod);

Session session = HibernateUtil.getSessionFactory().openSession();//exception inside
session.beginTransaction();
session.update(student);
session.getTransaction().commit();


}
}

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 final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}

}

当我调用主类时,我看到了以下堆栈跟踪:

Failed to create sessionFactory object.java.lang.NullPointerException
Exception in thread "main" java.lang.ExceptionInInitializerError
at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
at logic.HibernateUtil.<clinit>(HibernateUtil.java:10)
at logic.Main.main(Main.java:12)
Caused by: java.lang.NullPointerException
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:78)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2293)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2289)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1758)
at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
... 2 more

谁知道问题的原因是什么?

更新

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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.connection.url">"jdbc:sqlserver://10.16.9.52:1433;databaseName=ForHiberTest;"</property>
<property name="hibernate.connection.username">userNew</property>
<property name="hibernate.connection.password">Pass12345</property>
<property name="show_sql">true</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<mapping class="logic.Prepod"></mapping>
<mapping class="logic.Student"></mapping>
</session-factory>
</hibernate-configuration>

最佳答案

您错过了 hibernate.cfg.xml 文件中的方言属性

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

关于java - 无法创建 sessionFactory 对象.java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18744117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com