Reputation: 608
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;
}
Upvotes: 15
Views: 71823
Reputation: 181
@id annotation in entity class,
In My case forgot to add @id Annotation after all correct configuration, once i add the annotation its working fine
@Entity
public class demo{
@Id
private long id
}
Upvotes: 0
Reputation: 3367
In my case I was trying to connect to DB which is Mysql8+ version from my Java application. I face the same issue. The error logs are same but the reason for failure is different.
I made the change in my.cnf file of Mysql and it is: max-connect-errors=1000
Default value is 100 but it wont work. So increases it to 1000, it starts working.
Need to look the logs in details in case of failure. If the logs contains this anywhere then try this thing.
java.sql.SQLException: null, message from server: "Host 'X' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"
Upvotes: 0
Reputation: 5853
This can also happen if the dialect cannot connect for any other reason, such as password expired.
Check for any earlier exceptions that may reveal the cause.
Upvotes: 0
Reputation: 1
The configuration object was reading all the properties from the hibernate.cfg.xml
file. But the StandardServiceRegistryBuilder
object was not using those properties.
This solution worked for me. The below statement is critical in making this work:
ServiceRegistry serviceRegistry =
new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
Full solution:
package demo.jaxrs.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
System.out.println("Properties: " + configuration.getProperties());
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// Create the SessionFactory from hibernate.cfg.xml
return configuration.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Upvotes: 0
Reputation: 1749
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()
.
Upvotes: 25
Reputation: 137
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.
Upvotes: 8
Reputation: 1917
As he say about ServiceRegistry :
package org.phenix.hibernate.main;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// Create the SessionFactory from hibernate.cfg.xml
return configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Upvotes: 0
Reputation: 12167
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);
Upvotes: 1
Reputation: 372
Thanks for the solution. Somehow the 4.3.5 does not take the connection (Dialect) information from the hibernate.cfg.xml, use hibernate.properties file for that.
Upvotes: 0
Reputation: 25004
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.
Upvotes: 5
Reputation: 12890
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");
Upvotes: 2