0

I was trying to connect a simple java application with mysql db using hibernate. I have already created the schema in my db and this java application is simply creating a table in this schema and inserting data in it.All the time I am getting same error. My Code is as follows:

hibernate.cfg.xml

hibernate.cfg.xml

UserDetails.java

enter image description here

HibernateTest.java

enter image description here

Error:

enter image description here

Please help me, I have been stuck for quite a long time.

Thanks !!

2
  • There are not serious problems with stuff are showed by you. Please, add your libraries list. Commented Mar 11, 2016 at 22:15
  • Please post the code and error messages as text, not as images. Commented Mar 12, 2016 at 10:40

2 Answers 2

1

Seems like a duplicate of Exception in thread "main" java.util.ServiceConfigurationError

Seems that your using hibernate >=4 and the setup procedure from hibernate <4. Correct way according to the link is.

Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Session session = sessionFactory.openSession();
Sign up to request clarification or add additional context in comments.

5 Comments

I am not able to find any class named "ServiceRegistryBuilder".
What's your hibernate version.
thanks ! this worked for hibernate 4 , but still shows same error for hibernate % release.
Take a look into the hibernate 5 documentation -> docs.jboss.org/hibernate/orm/5.0/quickstart/html
@SuyashTilhari new Configuration().configure().buildSessionFactory(); is the correct way of Hibernate 4 and Hibernate 5 configuration. Why do you accept this answer?
1
//check External Jar files whether you added properly or not...if not remove all jar files and add once again...

After Main

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.OpenSession();
UserDetails ud = new UserDetails();
ud.setName("bdskbf");
ud.setId(23);
Transaction tnx = session.beginTransaction();
session.save(ud);
tnx.getTransaction.commit();

1 Comment

cfg.configure("hibernate.cfg.xml"); does the same as cfg.configure(); does. sf.OpenSession(); is incorrect, as you know :)

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.