rajamit872

Hibernate 1st Application(to saveOrUpdate) And Select

Feb 12th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. //HIbernate First(Beginer level) Application to Perform Persistence Logic
  2. //Required Table:Employee
  3. //Four Column::EID,,FIRSTNAME, LASTNAME, EMAIL
  4. //DataBAse Software--Oracle 11g
  5. //Set all The Jar files to the Eclipse ID(ojdbc6.jar and all the jar files that is in hibernate software)
  6.  
  7.  
  8. //Employee.hbm.xml
  9.  
  10. <?xml version="1.0"?>
  11. <!DOCTYPE  hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
  12. <hibernate-mapping>
  13.     <class name="com.nt.pojo.EmpBean"  table="Employee">
  14.         <id  name="no" column="EID"/>  
  15.         <property  name="fname" column="FIRSTNAME"/>
  16.         <property  name="lname" column="LASTNAME"/>
  17.         <property  name="email" column="EMAIL"/>
  18.     </class>
  19. </hibernate-mapping>
  20.  
  21.  
  22. //hibernate.cfg.xml
  23.  
  24. <!DOCTYPE hibernate-configuration PUBLIC
  25.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  26.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  27. <hibernate-configuration>
  28.     <session-factory>
  29.     <property name = "hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  30.     <property name = "hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
  31.     <property name = "hibernate.connection.username">scott</property>
  32.     <property name = "hibernate.connection.password">tiger</property>
  33.     <property name="hibernate.show_sql">true</property>
  34.    
  35.     <mapping resource="com/nt/cfgs/Employee.hbm.xml"/>
  36.    </session-factory>
  37. </hibernate-configuration>
  38.  
  39.  
  40. //EmpBean.java
  41.  
  42. package com.nt.pojo;
  43. public class EmpBean {
  44.     private int no;
  45.     private String fname,lname,email;
  46.    
  47.     public EmpBean(){
  48.         System.out.println("Zero param Constructor");
  49.     }
  50.  
  51.     public int getNo() {
  52.         return no;
  53.     }
  54.    
  55.  
  56.     public void setNo(int no) {
  57.         this.no = no;
  58.     }
  59.  
  60.     public String getFname() {
  61.         return fname;
  62.     }
  63.  
  64.     public void setFname(String fname) {
  65.         this.fname = fname;
  66.     }
  67.  
  68.     public String getLname() {
  69.         return lname;
  70.     }
  71.  
  72.     public void setLname(String lname) {
  73.         this.lname = lname;
  74.     }
  75.  
  76.     public String getEmail() {
  77.         return email;
  78.     }
  79.  
  80.     public void setEmail(String email) {
  81.         this.email = email;
  82.     }
  83. }//class
  84.  
  85. //TestClient.java
  86. //To Save or Uodate the data into Employee Table
  87. //Run This Application to perform the operation
  88.  
  89. package com.nt.test;
  90.  import org.hibernate.Session;
  91. import org.hibernate.SessionFactory;
  92. import org.hibernate.Transaction;
  93. import org.hibernate.cfg.Configuration;
  94.  
  95. import com.nt.pojo.EmpBean;
  96.  
  97. public class TestClient {
  98.     public static void main(String[] args) throws Exception{
  99.        
  100.         Configuration cfg = new Configuration();
  101.            
  102.         cfg = cfg.configure("/com/nt/cfgs/hibernate.cfg.xml");
  103.        
  104.         SessionFactory factory = cfg.buildSessionFactory();
  105.        
  106.         Session ses = factory.openSession();
  107.        
  108.         Transaction tx=null;
  109.         try{
  110.         tx = ses.beginTransaction();
  111.         EmpBean eb = new EmpBean();
  112.         eb.setNo(111);
  113.         eb.setFname("Rajesh");
  114.         eb.setLname("Amarnath");
  115.         eb.setEmail("[email protected]");
  116.        
  117.    
  118.         ses.saveOrUpdate(eb);
  119.         tx.commit();
  120.         }
  121.         catch(Exception e){
  122.             tx.rollback();  
  123.             e.printStackTrace();
  124.         }
  125.         ses.close();
  126.         factory.close();
  127.     }
  128. }
  129.  
  130.  
  131. //ReadObjTest.java
  132. //Run This Application to perform Persistence logic
  133. //Select Operation in HIbernate
  134. package com.nt.test;
  135.  
  136. import java.util.Scanner;
  137. import org.hibernate.Session;
  138. import org.hibernate.SessionFactory;
  139. import org.hibernate.cfg.Configuration;
  140.  
  141. import com.nt.pojo.EmpBean;
  142.  
  143. public class ReadObjTest {
  144.     public static void main(String[] args) {
  145.         //activate HB Framework
  146.         Configuration cfg = new Configuration();
  147.        
  148.         cfg = cfg.configure("/com/nt/cfgs/hibernate.cfg.xml");
  149.        
  150.         //Create SessionFactory objects
  151.         SessionFactory factory = cfg.buildSessionFactory();
  152.        
  153.         //Create Session Object
  154.         Session ses = factory.openSession();
  155.        
  156.     /*  //Load object By using ses.get(-,-) method
  157.         EmpBean eb = (EmpBean)ses.load(EmpBean.class, 101);
  158.        
  159.         if(eb!=null)
  160.         {
  161.             System.out.println(eb.getNo() + "    " + eb.getFname()+"    " + eb.getLname() + "   " + eb.getEmail());
  162.            
  163.         }
  164.         else
  165.             System.out.println("Record Not Found");
  166.         */
  167.        
  168.         //Entering the value of id whose data need to be retrieved
  169.         Scanner sc = new Scanner(System.in);
  170.         System.out.println("Enter  id ::");
  171.         int sno = sc.nextInt();
  172.         try{
  173.             EmpBean  eb = (EmpBean)ses.load(EmpBean.class, sno);
  174.             System.out.println(eb.getNo() + "    " + eb.getFname()+"    " + eb.getLname() + "   " + eb.getEmail());
  175.         }
  176.         catch(Exception e ){
  177.             System.out.println("Record Not Found");
  178.         }
  179.        
  180.         //Close Object
  181.         ses.close();
  182.         factory.close();
  183.         }//main
  184.     }//class
Advertisement
Add Comment
Please, Sign In to add comment