Insert record for Mysql database thru hibernate but for part 1 not use hibernate query language and use session object for save data to Database table.
In this example no need to create table for transaction if we use “
1.First we need to create connecton poolwhich is used for connecting database server and add mapping resources (see Configuring Hibernate).
2.Create POJO class with relevant data filed with to be saved in Table.(see Writing First Persistence Class)
3.Set mapping between created POJO class data base Table(see Mapping the Contact Object to the Database Contact table)
4.Finally test hibernate example (see Developing Code to Test Hibernate example)
Configuring Hibernate
Hibernate uses the hibernate.cfg.xml to create the connection pool and setup required environment.
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost/hibernatetutorial
Writing First Persistence Class
Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column. Here is the code for
Contact.java:
package xx.xxx.hibernate;
public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;
public String getEmail() {
return email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setEmail(String string) {
email = string;
}
public void setFirstName(String string) {
firstName = string;
}
public void setLastName(String string) {
lastName = string;
}
public long getId() {
return id;
}
public void setId(long l) {
id = l;
}
Mapping the Contact Object to the Database Contact table
The file contact.hbm.xml is used to map Contact Object to the Contact table in the database. Here is the code for contact.hbm.xml:
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Developing Code to Test Hibernate example
Now we are ready to write a program to insert the data into database. We should first understand about the Hibernate's Session. Hibernate Session is the main runtime interface between a Java application and Hibernate. First we are required to get the Hibernate Session.SessionFactory allows application to create the Hibernate Sesssion by reading the configuration from hibernate.cfg.xml file. Then the save method on session object is used to save the contact information to the database:
Here is the code of FirstExample.java
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
public class FirstExample {
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
try{
// This step will read hibernate.cfg.xml
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
tx = session.beginTransaction();
Contact contact = new Contact();
contact.setId(111);
contact.setFirstName("Suresh");
contact.setLastName("Rangana");
contact.setEmail("sureshpdn@yahoo.com");
session.save(contact);
tx.commit();//write to table
}catch(Exception e){
System.out.println(e.getMessage());
tx.rollback();
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}

No comments:
Post a Comment