| Author |
unable to insert data in mysql through hibernate even program run without error
|
krishan gopal gupta
Greenhorn
Joined: Dec 30, 2012
Posts: 8
|
|
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();
maintest.createAndStoreUserInfo();
HibernateUtil.getSessionFactory().close();
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();
userInfo.setUserId("1");
userInfo.setFirstName("krishan");
userInfo.setLastName("Gupta");
userInfo.setEmail("krishang@cdac.in");
userInfo.setMobile("9823916991");
System.out.println(userInfo.getUserId());
session.getTransaction().commit();
//txn.commit();
System.out.println("Create function End here ");
}
}
hibernet.util.java
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(){
try{
return new Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
System.out.println("Inital SessionFactory creation fail" + ex);
return null;
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
userinfo.java
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) {
this.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) {
this.mobile = mobile;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
hibernet.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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="hibernate.hbm2ddl.auto">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"/>
</session-factory>
</hibernate-configuration>
hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cdac.user.UserInfo" table="user_info">
<id name="userId" column="user_id" type="string">
<generator class="assigned" />
</id>
<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"/>
</class>
</hibernate-mapping>
i did not get any idea why this happen. i am using netbean IDE .please help me out.
Thanks in advance
|
 |
Vishal Shaw
Ranch Hand
Joined: Aug 09, 2012
Posts: 179
|
|
Hi,
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 userinfo.java while Class name is Userinfo.
- Most importantly, it's not "hibernet" , it's Hibernate
Vishal
|
Programming is about thinking, NOT coding
|
 |
krishan gopal gupta
Greenhorn
Joined: Dec 30, 2012
Posts: 8
|
|
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
Joined: Aug 09, 2012
Posts: 179
|
|
|
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
|
 |
 |
|
|
subject: unable to insert data in mysql through hibernate even program run without error
|
|
|