Saturday, 20 July 2013

ALab24A : One-to-one Uni Directional (Foreign Key)


1. hibernate.cfg.xml
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
   <session-factory>
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3333/practice</property>
     <property name="hibernate.connection.username">root</property>
     <property name="hibernate.connection.password">admin</property>
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
     <property name="hibernate.show_sql">true</property>
     <mapping class="com.persistence.Student"/>
     <mapping class="com.persistence.Address"/>
   </session-factory>
 </hibernate-configuration>
2. Util.java
 package com.util;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.AnnotationConfiguration;
 public class Util {
     static SessionFactory sf;
     static{
         try{
             AnnotationConfiguration acfg = new AnnotationConfiguration();
             acfg = (AnnotationConfiguration)acfg.configure();
             sf = acfg.buildSessionFactory();
         }catch (Exception e) {
             e.printStackTrace();
         }
     }
     public static SessionFactory getSF(){
         return sf;
     }
 }
3. Student.java
 package com.persistence;
 import javax.persistence.*;
 @Entity
 @Table(name="student")
 public class Student {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="sid")
     private int sid;
     @Column(name="sname")
     private String sname;
     @OneToOne
     @JoinColumn(name="adid")
     private Address address;
     public Student() {
         super();
     }
     public Student(String sname) {
         super();
         this.sname = sname;
     }
     //Getters & Setters
 }
4. Address.java
 package com.persistence;
 import javax.persistence.*;
 @Entity
 @Table(name="address")
 public class Address {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="adid")
     private int adid;
     @Column(name="street")
     private String street;
     @Column(name="city")
     private String city;
     @Column(name="state")
     private String state;
     public Address() {
         super();
     }
     public Address(String street, String city, String state) {
         super();
         this.street = street;
         this.city = city;
         this.state = state;
     }
     //Getters & Setters
 }
5. L24AInsert.java
 package com.persistence;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import com.util.Util;
 public class AL24AInsert {
     public static void main(String[] args) {
         Transaction tx = null;
         try{
             SessionFactory sf = Util.getSF();
             Session s = sf.openSession();
             tx = s.beginTransaction();
             Student stu = new Student("Ram");
             s.save(stu);
             Student stu1 = new Student("Shyam");
             s.save(stu1);
             Student stu3 = new Student("Tarek");
             s.save(stu3);
             Student stu4 = new Student("Umesh");
             s.save(stu4);
             Student stu5 = new Student("Vinit");
             s.save(stu5);
             Student stu6 = new Student("Walter");
             s.save(stu6);
             Address add = new Address("20","Bangalore","KA");
             s.save(add);
             Address add1 = new Address("40","Delhi","DL");
             s.save(add1);
             Address add3 = new Address("80","Hyderabad","AP");
             s.save(add3);
             Address add4 = new Address("90","Indore","MP");
             s.save(add4);
             Address add5 = new Address("100","Jaipur","RS");
             s.save(add5);
             Address add6 = new Address("21","Lucknow","UP");
             s.save(add6);
             stu.setAddress(add1); //one Student-to-one Address
             stu1.setAddress(add); //one Student-to-one Address
             stu3.setAddress(add4); //one Student-to-one Address
             stu4.setAddress(add3); //one Student-to-one Address
             stu5.setAddress(add6); //one Student-to-one Address
             stu6.setAddress(add5); //one Student-to-one Address
             tx.commit();
         }catch(Exception e){
             e.printStackTrace();
             if(tx!=null)
                 tx.rollback();
         }
     }
 }
6. L24ASelect.java
 package com.persistence;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import com.util.Util;
 public class AL24ASelect {
     public static void main(String[] args) {
         Transaction tx = null;
         try{
             SessionFactory sf = Util.getSF();
             Session s = sf.openSession();
             tx = s.beginTransaction();
             for(int i=1;i<=6;i++){
                 Student stu2 = (Student)s.load(Student.class,i);
                 System.out.println(stu2.getSid()+" "+stu2.getSname()+" "
                         +stu2.getAddress().getAdid()+" "
                         +stu2.getAddress().getStreet()+" "
                         +stu2.getAddress().getCity()+" "
                         +stu2.getAddress().getState());
             }
             tx.commit();
         }catch(Exception e){
             e.printStackTrace();
             if(tx!=null)
                 tx.rollback();
         }
     }
 }
7. Table
 use practice;
 drop table student;
 drop table address;
 create table student(sid int primary key auto_increment, sname char(15), adid int);
 create table address(adid int primary key auto_increment, street char(15), city char(15), state char(15));
 desc student;
 desc address;
 select * from student;
 select * from address;

No comments:

Post a Comment