Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HIbernate First(Beginer level) Application to Perform Persistence Logic
- //Required Table:Employee
- //Four Column::EID,,FIRSTNAME, LASTNAME, EMAIL
- //DataBAse Software--Oracle 11g
- //Set all The Jar files to the Eclipse ID(ojdbc6.jar and all the jar files that is in hibernate software)
- //Employee.hbm.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
- <hibernate-mapping>
- <class name="com.nt.pojo.EmpBean" table="Employee">
- <id name="no" column="EID"/>
- <property name="fname" column="FIRSTNAME"/>
- <property name="lname" column="LASTNAME"/>
- <property name="email" column="EMAIL"/>
- </class>
- </hibernate-mapping>
- //hibernate.cfg.xml
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name = "hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name = "hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
- <property name = "hibernate.connection.username">scott</property>
- <property name = "hibernate.connection.password">tiger</property>
- <property name="hibernate.show_sql">true</property>
- <mapping resource="com/nt/cfgs/Employee.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- //EmpBean.java
- package com.nt.pojo;
- public class EmpBean {
- private int no;
- private String fname,lname,email;
- public EmpBean(){
- System.out.println("Zero param Constructor");
- }
- public int getNo() {
- return no;
- }
- public void setNo(int no) {
- this.no = no;
- }
- public String getFname() {
- return fname;
- }
- public void setFname(String fname) {
- this.fname = fname;
- }
- public String getLname() {
- return lname;
- }
- public void setLname(String lname) {
- this.lname = lname;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }//class
- //TestClient.java
- //To Save or Uodate the data into Employee Table
- //Run This Application to perform the operation
- package com.nt.test;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.nt.pojo.EmpBean;
- public class TestClient {
- public static void main(String[] args) throws Exception{
- Configuration cfg = new Configuration();
- cfg = cfg.configure("/com/nt/cfgs/hibernate.cfg.xml");
- SessionFactory factory = cfg.buildSessionFactory();
- Session ses = factory.openSession();
- Transaction tx=null;
- try{
- tx = ses.beginTransaction();
- EmpBean eb = new EmpBean();
- eb.setNo(111);
- eb.setFname("Rajesh");
- eb.setLname("Amarnath");
- ses.saveOrUpdate(eb);
- tx.commit();
- }
- catch(Exception e){
- tx.rollback();
- e.printStackTrace();
- }
- ses.close();
- factory.close();
- }
- }
- //ReadObjTest.java
- //Run This Application to perform Persistence logic
- //Select Operation in HIbernate
- package com.nt.test;
- import java.util.Scanner;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import com.nt.pojo.EmpBean;
- public class ReadObjTest {
- public static void main(String[] args) {
- //activate HB Framework
- Configuration cfg = new Configuration();
- cfg = cfg.configure("/com/nt/cfgs/hibernate.cfg.xml");
- //Create SessionFactory objects
- SessionFactory factory = cfg.buildSessionFactory();
- //Create Session Object
- Session ses = factory.openSession();
- /* //Load object By using ses.get(-,-) method
- EmpBean eb = (EmpBean)ses.load(EmpBean.class, 101);
- if(eb!=null)
- {
- System.out.println(eb.getNo() + " " + eb.getFname()+" " + eb.getLname() + " " + eb.getEmail());
- }
- else
- System.out.println("Record Not Found");
- */
- //Entering the value of id whose data need to be retrieved
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter id ::");
- int sno = sc.nextInt();
- try{
- EmpBean eb = (EmpBean)ses.load(EmpBean.class, sno);
- System.out.println(eb.getNo() + " " + eb.getFname()+" " + eb.getLname() + " " + eb.getEmail());
- }
- catch(Exception e ){
- System.out.println("Record Not Found");
- }
- //Close Object
- ses.close();
- factory.close();
- }//main
- }//class
Advertisement
Add Comment
Please, Sign In to add comment