John Kent
John Kent

Reputation: 41

Hibernate and data insertion into database fails

<hibernate-configuration>
<session-factory>  <property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">

jdbc:mysql://localhost/products</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password"></property>
  <property name="hibernate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- Mapping files -->
  <mapping resource="prod.hbm.xml"/>
</session-factory>
</hibernate-configuration>

i am using the above XML as hibernate config file to insert into my database named "Products" some data. But it displays a compile error:Connection cannot be null when 'hibernate.dialect' not set I have no idea why/how to fix. Could some of you offer me some ideas to move on ? I am grateful. Thank you.

[UPDATE]

I think I should include steps in which I created and set up my simple project, I guess it might be the cause

I already had DB and table in mysql; I then created a empty java project, I added source file (java extensions), then I added 2 xml files (hibernate.cfg.xml and prod.hbm.xml). Then I just compiled in hope that I would run.

And here is the message I have got from commmand prompt after trying to run my application as a java application

Jul 13, 2012 12:32:55 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Jul 13, 2012 12:32:55 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.2.Final}
Jul 13, 2012 12:32:55 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 13, 2012 12:32:55 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 13, 2012 12:32:56 PM org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Connection cannot be null when 'hibernate.dialect' not set

Upvotes: 2

Views: 7010

Answers (6)

user2748194
user2748194

Reputation: 11

I just spent 2 days trying to fix this masty bug it so happends that in standalone.xml file of jboss it was not connecting to one of the datasources and instead of saying that, it throws this erroneous error so beware of this jboss.

Upvotes: 1

Dhirendra Khanka
Dhirendra Khanka

Reputation: 849

Its because Hibernate is not able to read all of your properties from the hibernate.cfg.xml. Even i was having this same problem. If you can see the java console startup messages, it says

Jul 13, 2012 12:32:56 PM org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: ***No appropriate connection provider encountered, assuming application will be supplying connections***
Connection cannot be null when 'hibernate.dialect' not set

It means that, the system is assuming that you will be configuring the properties from application. e.g hibernate.cfg.xml does not find hibernate.properties file

It was so happening in my case that only "mapping resource" property was looked up from this xml rest no other properties were read. So i added another hibernate.properties file in my class path and defined the connection properties there. And now it was reading all these properties from properties file instead of the xml. (You can always see what all properties have been read from the resources from Console window). So i had to make use of both of these xml and properties file. I am still unclear as to what the issue is exactly.

Upvotes: 0

Jaanus
Jaanus

Reputation: 16531

Don't forget to call configure()

Configuration configuration = new Configuration();
configuration.configure();

Upvotes: 0

Dick Chesterwood
Dick Chesterwood

Reputation: 2659

I suspect your configuration file is fine and that the fault is in the code you use to create your SessionFactory. They've mucked around with this in Hiberanate 4 and it has caused untold confusion.

This works for me:

private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory()
{
   if (sessionFactory == null)
   {
      Configuration configuration = new Configuration();

      ServiceRegistry serviceRegistry = new
              ServiceRegistryBuilder().applySettings
              (configuration.configure().getProperties()).buildServiceRegistry();        

      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
   }
   return sessionFactory;

}

I had the same error as you, I realised I was missing the important call to configuration.configure() in that ugly line of code defining the serviceRegistry.

(By the way, the error about hibernate.dialect is a red herring - if Hibernate can't find the database it can't work out the dialect, and it erroneously complains about the dialect rather than the underlying problem.)

Upvotes: 2

user1000535
user1000535

Reputation: 997

com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/Products root root 10 true
org.hibernate.dialect.MySQLDialect update

    <!-- Mapping files -->

    <mapping resource="prod.hbm.xml"/>
</session-factory>

Note: First line is very important one, check this doctype, even single space also gives some error Check ur DB has password, if it is specify that password.

Upvotes: 0

Subin Sebastian
Subin Sebastian

Reputation: 10997

it should be

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

Upvotes: 2

Related Questions