Saturday, 20 July 2013

ALab24 : Many-to-one Uni Directional


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.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
 import javax.persistence.Table;
 @Entity
 @Table(name="student")
 public class Student {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="sid")
     private int sid;
     @Column(name="sname")
     private String sname;
     @ManyToOne
     @JoinColumn(name="adid")
     private Address address;
     public Student() {
         super();
     }
     public Student(String sname) {
         super();
         this.sname = sname;
     }
     public int getSid() {
         return sid;
     }
     public void setSid(int sid) {
         this.sid = sid;
     }
     public String getSname() {
         return sname;
     }
     public void setSname(String sname) {
         this.sname = sname;
     }
     public Address getAddress() {
         return address;
     }
     public void setAddress(Address address) {
         this.address = address;
     }
 }
4. Address.java
 package com.persistence;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.Table;
 @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;
     }
     public int getAdid() {
         return adid;
     }
     public void setAdid(int adid) {
         this.adid = adid;
     }
     public String getStreet() {
         return street;
     }
     public void setStreet(String street) {
         this.street = street;
     }
     public String getCity() {
         return city;
     }
     public void setCity(String city) {
         this.city = city;
     }
     public String getState() {
         return state;
     }
     public void setState(String state) {
         this.state = state;
     }
 }
5. L24.java
 package com.persistence;
 import java.util.HashSet;
 import java.util.Set;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import com.util.Util;
 public class AL24 {
     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("18","Bangalore","KA");
             s.save(add);
             Set stuset=new HashSet();
             stuset.add(stu);
             stuset.add(stu1);
             stuset.add(stu3);
             stuset.add(stu4);
             stuset.add(stu5);
             stuset.add(stu6);
             for(Student st:stuset)
                 st.setAddress(add); //Many Student-to-one Address
             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();
         }
     }
 }
6. 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