Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Solution Is Here..complete Code Of FirstExample, Hibernate Runninge Xample

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

;
 
Java Examples for Beginners
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

package roseindia.tutorial.hibernate;

import java.util.List;

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


/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
FirstExample fe = new FirstExample();
Session session = null;
Transaction tx = null;
List list =null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
tx=session.beginTransaction();

//Insert or Update
System.out.println("Insert or Update data...\n");
fe.saveOrUpdate(session, tx);
//Fetching data...
System.out.println("Fetching data...\n");
fe.getContactRecords(list,session);

System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
public void saveOrUpdate(Session session,Transaction tx) {
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");

// TODO Auto-generated method stub
Contact contact = new Contact();
contact.setId(7);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.saveOrUpdate(contact);
tx.commit();
System.out.println("\n before update");
}
public void getContactRecords(List list,Session session){
Query query = session.createQuery("from roseindia.tutorial.hibernate.Contact");
/** execute query and get result in form of Java objects */
list = query.list();
System.out.println("intial value---> "+list.get(0));
if(list.size() >0){
int i=0;
for(;i<list.size();i++)
{
Contact c = (Contact)list.get(i);

System.out.println(c.getId());
System.out.println(c.getFirstName());
System.out.println(c.getLastName());
System.out.println(c.getEmail());

}
}
}
}

And one more important thing is we need to provide as
In hibernate.cfg.xml:

<property name="hibernate.connection.password">root</property>

After running successfully please check in DB for the row insertion.


Regards
Narendra

Posted by Narendra on Monday, 02.22.10 @ 19:44pm | #95050

Ask programming questions?

 

 

Back to Tutorial
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.