Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Creating a Hibernate Test Project using maven.

when i run the project, it generates Exception:

org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.hibernatetest.App.main(App.java:33)
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.hibernatetest.App.main(App.java:51) 

But in main class required properties are set.don't know why programme is genrAating exception.

Main Class:

public class App {

public static void main(String[] args) {
    Configuration configuration;
    ServiceRegistry serviceRegistry;
    SessionFactory sessionFactory;
    Session session = null;
    try {
        configuration = new Configuration();


        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        configuration.setProperty("hibernate.dialect ", "com.applerao.hibernatesqlite.dialect.SQLiteDialect");
        configuration.setProperty("hibernate.connection.url ", "jdbc:sqlite:TailorDB.db");
        configuration.setProperty("hibernate.connection.driver_class ", "org.sqlite.JDBC");
        configuration.setProperty("hibernate.connection.username ", "");
        configuration.setProperty("hibernate.connection.password ", "");
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        session = sessionFactory.openSession();

        CustomerModel c = new CustomerModel();
        c.setID(5);
        c.setNIC_Number("691201631345");
        c.setFirstName("Zee");
        c.setNumber("55225522");
        c.setLastName("Jan");
        c.setCustomerCode("Zee-123");

        session.beginTransaction();
        session.save(c);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
}
}

In POM file:

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.7.2</version>
    </dependency>
    <dependency>
        <groupId>com.applerao</groupId>
        <artifactId>hibernatesqlite</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

Any idea where the problem can be??

CustomerModel cLass:

@Entity
@Table(name = "Customer")

public class CustomerModel {

@Id
@GeneratedValue
@Column(name = "c_id")
int ID;
@Column(name = "c_code")
String customerCode;
@Column(name = "c_fname")
String firstName;
@Column(name = "c_mname")
String middleName;
@Column(name = "c_lname")
String lastName;
@Column(name = "c_nic")
String NIC_Number;
@Column(name = "c_email")
String email;
@Column(name = "c_pnumber")
String number;
}
like image 222
Sameer Azeem Avatar asked Jan 22 '14 12:01

Sameer Azeem


5 Answers

You didn't initialize configuration, serviceRegistry, and sessionFactory correctly. From Hibernate 4.3.2.Final, StandardServiceRegistryBuilder is introduced. Please follow this order to initialize, e.g.

    Configuration configuration = new Configuration();
    configuration.configure("com/jeecourse/config/hibernate.cfg.xml");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

So in your code, you missed the step: configuration.configure().

like image 82
Jimmy Avatar answered Nov 07 '22 08:11

Jimmy


This seems to be old question, but now when I am using 4.3.1 final Hibernate version I also came across this problem. After reading through many answers it seemed like they have really not taken care of it in the documentation but I went through the documentation and i think they have necessary information. It is not necessary that hibernate configuration needs to be in a .properties file, but it can be in xml file also.

Just make sure you invoke configure() before invoking build() on the instance of StandardServiceRegistryBuilder. This is because configure() actually loads the configuration from the default cfg.xml file and build actually uses it.

To understand this, you can do one thing....before invoking configure() .... invoke getSettings() on the intance of StandardServiceRegistryBuilder and print it...it's a Map.

You will not see any hibernate properties you have mentioned in the cfg.xml

Now invoke configure() and print getSettings() map....you will see...you have got all your properties.

like image 44
Omkar Patkar Avatar answered Nov 07 '22 09:11

Omkar Patkar


In hibernate 4.3, it seems that need to use hibernate.properties to do config, use hibernate.cfg.xml only to include the .hbm.xml files, so, following is the solution:

in classpath, add a file: hibernate.properties and do all config here, e.g:

# jdbc connecition
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost:5432/xdm
hibernate.connection.username = postgres
hibernate.connection.password = 123456

# dialect
hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect

# c3p0
hibernate.c3p0.min_size = 2
hibernate.c3p0.max_size = 5
hibernate.c3p0.max_statements = 20
hibernate.jdbc.batch_size = 10
hibernate.c3p0.timeout = 300
hibernate.c3p0.idle_test_period = 3000
hibernate.c3p0.testConnectionOnCheckout = true

# other
hibernate.show_sql = true
hibernate.max_fetch_depth = 3

then, in hibernate.cfg.xml, only include the .hbm.xml files, e.g:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- mapping files -->
        <mapping resource="hibernate/model/Event.hbm.xml" />
    </session-factory>
</hibernate-configuration>

tip: the official document didn't give any tip like this, I think it's a very bad thing.

like image 42
user218867 Avatar answered Nov 07 '22 09:11

user218867


Set the configuration properties before applying configuration properties to the settings of StandardServiceRegistryBuilder

    configuration.setProperty("hibernate.dialect", "com.applerao.hibernatesqlite.dialect.SQLiteDialect");
    configuration.setProperty("hibernate.connection.url", "jdbc:sqlite:TailorDB.db");
    configuration.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC");
    configuration.setProperty("hibernate.connection.username", "");
    configuration.setProperty("hibernate.connection.password", "");
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Also, there seems to be a space at the end of the property keys while setting them. Please remove them.

Based on the link, try changing this

configuration.setProperty("hibernate.dialect", "com.applerao.hibernatesqlite.dialect.SQLiteDialect");

to

configuration.setProperty("dialect", "com.applerao.hibernatesqlite.dialect.SQLiteDialect");
like image 2
Keerthivasan Avatar answered Nov 07 '22 09:11

Keerthivasan


Here is what I did to make all the configurations in the code without hibernate.cfg.xml or hibernate.properties. Tested with hibernate version 4.3.8.Final. Hope it would be helpful.

    Configuration configuration = new Configuration();

    Properties properties = new Properties();

    properties.put(Environment.DRIVER,
            com.mysql.jdbc.Driver.class.getName());
    properties.put(Environment.USER, "root");
    properties.put(Environment.PASS, "root");
    properties.put(Environment.HBM2DDL_AUTO, "create");
    properties.put(Environment.DIALECT, MySQL5Dialect.class.getName());
    properties
            .put(Environment.URL, "jdbc:mysql://localhost/hibernate_test");
    properties.put(Environment.SHOW_SQL, true);

    configuration.setProperties(properties);

    configuration.addAnnotatedClass(Stock.class).addAnnotatedClass(
            StockDailyRecord.class);

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();

    SessionFactory sessionFactory = configuration
            .buildSessionFactory(serviceRegistry);
like image 1
alijandro Avatar answered Nov 07 '22 08:11

alijandro