Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

unable to insert data in mysql through hibernate even program run without error

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello , I am new to hibernet. i tried sample program it run with out error but data is not inserted in mysql data base .below is code given package com.cdac.test;
import com.cdac.util.HibernateUtil;
import com.cdac.user.UserInfo;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainTest {

public static void main(String [] args){
MainTest maintest = new MainTest();
System.out.println("main Function end here");


private void createAndStoreUserInfo(){
System.out.println("Create function start here ");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction txn= session.beginTransaction();

UserInfo userInfo = new UserInfo();
System.out.println("Create function End here ");


package com.cdac.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

* @author krishan
* To get hibernate.session instance here
public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory(){

return new Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
System.out.println("Inital SessionFactory creation fail" + ex);
return null;

public static SessionFactory getSessionFactory(){

return sessionFactory;

package com.cdac.user;

* @author krishan
* This Class contain user related info its plain POJO class
public class UserInfo {
private String userId;
private String firstName;
private String lastName;
private String mobile;
private String email;

public String getEmail() {
return email;

public void setEmail(String email) { = email;

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getLastName() {
return lastName;

public void setLastName(String lastName) {
this.lastName = lastName;

public String getMobile() {
return mobile;

public void setMobile(String mobile) { = mobile;

public String getUserId() {
return userId;

public void setUserId(String userId) {
this.userId = userId;



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">;
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/user</property>
<property name="hibernate.connection.username">krishang</property>
<property name="hibernate.connection.password">*********</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<property name="">update</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.connection.autocommit">true</property>
<mapping resource="./hibernate.hbm.xml"/>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">;
<class name="com.cdac.user.UserInfo" table="user_info">
<id name="userId" column="user_id" type="string">

<generator class="assigned" />

<property name="firstName" type="string" column="user_firstname" not-null="true" length="25"/>
<property name="lastName" type="string" column="user_lastname" not-null="false" length="50"/>
<property name="mobile" type="string" column="user_mobile" not-null="false" length="50"/>
<property name="email" type="string" column="user_email" not-null="false" length="100"/>



i did not get any idea why this happen. i am using netbean IDE .please help me out.

Thanks in advance

Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To begin with use code tags for your code.Without it most of the ranchers won't take the trouble of looking at your code.

As for the code (yes, I took the pain), there are certain problems
- Your POJO is not Serializable
- You did not save the POJO object in the session , neither did you saved the session
- Your file name is while Class name is Userinfo.
- Most importantly, it's not "hibernet" , it's Hibernate

krishan gopal gupta
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vishal , now my code is working.

Next time i will be careful and use code tag and spell check before posting.. i apologies for that.

if you know any good book to stare Hibernate please suggest me ...

once again thanks ...
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there are plenty of books . I am sure if you search online, you'll get a lot of names. You can also study online tutorials
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic