-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Data not saved in MySQL
PostPosted: Mon Nov 01, 2010 6:41 am 
Newbie

Joined: Tue Oct 26, 2010 5:37 am
Posts: 5
Hi friends,
i am new to hibernate. just went through an example found in roseindia.
it deals with a simple contact saving.

i followed their notes and all works fine. but when looking for data in MySQL databse, it seems empty..

My code is given below

Main class
----------
package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_");
session.save(contact);
System.out.println("Done");
System.out.println(contact.getLastName());
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
System.out.println("OK");
}

}
}

contact class
--------------

package roseindia.tutorial.hibernate;


public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;

/**
* @return Email
*/
public String getEmail() {
return email;
}

/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}

/**
* @param string Sets the Email
*/
public void setEmail(String string) {
email = string;
}

/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}

/**
* @return ID Returns ID
*/
public long getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(long l) {
id = l;
}

}

hibernate.cfg.xml
-------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

contact.hbm.xml
----------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Contact" table="contact">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>

<class name="roseindia.tutorial.hibernate.Book" table="book">
<id name="lngBookId" type="long" column="id" >
<generator class="increment"/>
</id>

<property name="strBookName">
<column name="bookname" />
</property>
</class>

<class name="roseindia.tutorial.hibernate.Insurance" table="insurance">
<id name="lngInsuranceId" type="long" column="ID" >
<generator class="increment"/>
</id>

<property name="insuranceName">
<column name="insurance_name" />
</property>
<property name="investementAmount">
<column name="invested_amount" />
</property>
<property name="investementDate">
<column name="investement_date" />
</property>
</class>



</hibernate-mapping>



can u say whts happening with this code

My output is:

Inserting Record
Done
Kumar
Hibernate: insert into contact (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
OK


Thanks in advance


Top
 Profile  
 
 Post subject: Re: Data not saved in MySQL
PostPosted: Mon Nov 01, 2010 10:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 11, 2009 2:26 am
Posts: 29
InnoDB? you need transaction :D

session =sessionFactory.openSession();
session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_");
session.save(contact);
session.getTransaction().commit();
System.out.println("Done");


Top
 Profile  
 
 Post subject: Re: Data not saved in MySQL
PostPosted: Tue Nov 02, 2010 5:44 am 
Newbie

Joined: Tue Oct 26, 2010 5:37 am
Posts: 5
hi stliu

thanks for your quick reply, but still my problem exists.
now i get an error saying

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getTransaction() is undefined for the type Session

can u say what to do?


Top
 Profile  
 
 Post subject: Re: Data not saved in MySQL
PostPosted: Tue Nov 02, 2010 6:38 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
I will give you a solution, but you need to learn Java programming language before going into Hibernate. It could be very useful :D

Code:
session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_");
session.save(contact);
transaction.commit();
System.out.println("Done");


This would compile. Still, I think that the advice given by stliu is a total nonsense, so it would be very interesting to hear from you if it helped.


Top
 Profile  
 
 Post subject: Re: Data not saved in MySQL
PostPosted: Tue Nov 02, 2010 7:40 am 
Newbie

Joined: Tue Oct 26, 2010 5:37 am
Posts: 5
Hi regular,

Thanks a lot.
It worked and data is inserted to table.

i am very happy on working first hibernate code.
can u suggest any good tutorial for learning hibernate ?


Top
 Profile  
 
 Post subject: Re: Data not saved in MySQL
PostPosted: Tue Nov 02, 2010 7:42 am 
Newbie

Joined: Tue Oct 26, 2010 5:37 am
Posts: 5
hello r,
i thought your name was regular...
sorry for that.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.