0

我正在写应用程序。此应用程序使用休眠来访问数据库。转到代码:我有这样的代码:

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();


    }
}

休眠实用程序:

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

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

更新

休眠.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>
4

1 回答 1

1

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

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
于 2013-09-11T15:50:54.370 回答