我正在写申请。此应用程序使用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();
}
}
当我调用main class时,我看到了以下stacktrace:
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>
答案 0 :(得分:1)
你错过了hibernate.cfg.xml文件中的dialect属性
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>