0

I am writing application. This application uses hibernate for acces in database. Go to code: I have so code:

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

}

When I invoke main class I saw following 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

Who understand what cause of the problem?

UPDATE

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>
4
  • and application see this file, because if i replace it i see another exception Commented Sep 11, 2013 at 14:46
  • 1
    you have missed dialect property. Please provide dialect as well Commented Sep 11, 2013 at 15:43
  • and also tell the location of your hibernate.cfg.xml file Commented Sep 11, 2013 at 15:47
  • I think I can right it as answer now Commented Sep 11, 2013 at 15:49

1 Answer 1

1

you have missed the dialect property in your hibernate.cfg.xml file

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.