inserting a record using hibernate
Hibernate Save
save in hibernate
1) Create a table Banner 2) Create a Bean in java and map this hibernate xml file 3) Save this bean using hibernate session and commit. 4) Add hibernate XML file in hibernate config XML file Above three steps will make a record inserted. 1) Create table banner ( id bigInt primary key, banner varchar(3000) ) 2) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ package com.banner.beans; public class Banner { private long id; private String banner; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getBanner() { return banner; } public void setBanner(String banner) { this.banner = banner; } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Below is Banner.Hib.xml file, make this file entry in Hibernate config file For Primary key auto generation we used below
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3) Insert a Record using hibernate -- below is the classe file which is used to save this record in database ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ package com.banner.insert; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.banner.beans.Banner; public class SaveBanner { public void saveProduct(Banner product) { System.out.println("Before Inserting Record"); Session session = null; try{ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); //Create new instance of Contact and set org.hibernate.Transaction transaction = session.beginTransaction(); // values in it by reading them from form object System.out.println("Inserting Record"); session.save(product); transaction.commit(); System.out.println(" Record inserted"); } catch(HibernateException he) { System.out.println(he); System.out.println(he.getMessage()); } finally{ // Actual contact insertion will happen at this step try{ session.flush(); session.close(); }catch (Exception e) { System.out.println(e); } } } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ See in db a new record is inserted ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4) Add this in hib config Sample hib config is as below
com.mysql.jdbc.Driver
jdbc:mysql://localhost/shop
root
narayana
100
true
org.hibernate.dialect.MySQLDialect
update
+++++++++++++++++++++++++++++++++++++END++++++++++++++++++++++++++++++++++++++++
Please send comments to
vgdarur.javafive@blogger.com
Copyright © 2008 - iForeRunner.com
http://www.iforerunner.com/