Lara

Hibernate

Saturday(10-12-2016)                                  app5

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Util

{

          private static SessionFactory sf;

         

          static

          {

                   Configuration c1 = new Configuration();

                   c1.configure();

                   sf = c1.buildSessionFactory();                 

          }

          public static Session getSession()

          {

                   return sf.openSession();

          }

}

package com.lara;

 

public class Person {

          private Integer personId;

          private String firstName;

          private String lastName;

          private Integer age;

          private Address address;

          public Integer getPersonId() {

                   return personId;

          }

          public void setPersonId(Integer personId) {

                   this.personId = personId;

          }

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public String getLastName() {

                   return lastName;

          }

          public void setLastName(String lastName) {

                   this.lastName = lastName;

          }

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

          public Address getAddress() {

                   return address;

          }

          public void setAddress(Address address) {

                   this.address = address;

          }

         

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager1 {

          public static void main(String[] args) {

                   Address a1 = new Address();

                   a1.setHouseNo("123/B");

                   a1.setStreetName("BTM");

                  

                   Person p1 = new Person();

                   p1.setPersonId(101);

                   p1.setFirstName("abc");

                   p1.setLastName("xyz");

                   p1.setAge(22);

                   p1.setAddress(a1);

                  

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   s1.save(p1);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

                  

          }

}

package com.lara;

 

public class Address {

          private String houseNo;

          private String streetName;

          public String getHouseNo() {

                   return houseNo;

          }

          public void setHouseNo(String houseNo) {

                   this.houseNo = houseNo;

          }

          public String getStreetName() {

                   return streetName;

          }

          public void setStreetName(String streetName) {

                   this.streetName = streetName;

          }

         

          @Override

          public String toString() {

                   // TODO Auto-generated method stub

                   return streetName;

          }

         

}

package com.lara;

 

import java.io.BufferedReader;

import java.io.FileReader;

 

import org.hibernate.Session;

 

public class Manager2 {

          public static void main(String[] args) throws Exception{

                   FileReader fin = new FileReader("test1.txt");

                   BufferedReader bin = new BufferedReader(fin);

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   Address address = null;

                   Person person = null;

                   String row = bin.readLine();

                   String[] data = null;

                   while(row != null)

                   {

                             data = row.split(",");

                             address = new Address();

                             person = new Person();

                             person.setPersonId(Integer.parseInt(data[0].trim()));

                             person.setFirstName(data[1].trim());

                             person.setLastName(data[2].trim());

                             person.setAge(Integer.parseInt(data[3].trim()));

                             address.setHouseNo(data[4].trim());

                             address.setStreetName(data[5].trim());

                             person.setAddress(address);

                             s1.save(person);

                             row = bin.readLine();

                   }

                   s1.getTransaction().commit();

                   System.out.println("done");

                  

          }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager3 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Person p1 = (Person) s1.get(Person.class, 108);

                   System.out.println("----------");

                   System.out.println(p1.getFirstName());

                   System.out.println(p1.getLastName());

                   System.out.println(p1.getAge());

                   Address a1 = p1.getAddress();

                   System.out.println(a1.getHouseNo());

                   System.out.println(a1.getStreetName());

          }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager4 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Person p1 = (Person) s1.load(Person.class, 108);

                   System.out.println("----------");

                   System.out.println(p1.getFirstName());

                   System.out.println(p1.getLastName());

                   System.out.println(p1.getAge());

                   Address a1 = p1.getAddress();

                   System.out.println(a1.getHouseNo());

                   System.out.println(a1.getStreetName());

          }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager5 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Person p1 = (Person) s1.get(Person.class, 109);

                   p1.setLastName("hello");

                   p1.setAge(33);

                   p1.getAddress().setStreetName("Jaya Nagar");

                  

                   s1.beginTransaction();

                   s1.update(p1);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

                  

          }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager6 {

          public static void main(String[] args) {

                   Person p1 = new Person();

                   p1.setPersonId(104);

                   p1.setFirstName("Rupa");

                   p1.setLastName("hello");

                   p1.setAge(22);

                   p1.setAddress(new Address());

                   p1.getAddress().setHouseNo("140/T");

                   p1.getAddress().setStreetName("Koramangala");

                  

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   s1.update(p1);

                   s1.getTransaction().commit();

                   System.out.println("done");

                  

          }

}

package com.lara;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

 

public class Manager7 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Criteria ctr = s1.createCriteria(Person.class);

                   ctr.add(Restrictions.like("lastName", "%b%"));

                   List<Person> list = ctr.list();

                   for(Person p1 : list)

                   {

                             System.out.print(p1.getPersonId() + ",");

                             System.out.print(p1.getFirstName() + ",");

                             System.out.print(p1.getLastName() + ",");

                             System.out.print(p1.getAge() + ",");

                             System.out.print(p1.getAddress().getHouseNo() + ",");

                             System.out.println(p1.getAddress().getStreetName() + ",");

                   }

          }

}

package com.lara;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

 

public class Manager8 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Criteria ctr = s1.createCriteria(Person.class);

                   ctr.add(Restrictions.like("address.streetName", "%BTM%"));

                   List<Person> list = ctr.list();

                   for(Person p1 : list)

                   {

                             System.out.print(p1.getPersonId() + ",");

                             System.out.print(p1.getFirstName() + ",");

                             System.out.print(p1.getLastName() + ",");

                             System.out.print(p1.getAge() + ",");

                             System.out.print(p1.getAddress().getHouseNo() + ",");

                             System.out.println(p1.getAddress().getStreetName() + ",");

                   }

          }

}

package com.lara;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

 

public class Manager9 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Criteria ctr = s1.createCriteria(Person.class);

                   ctr.add(Restrictions.like("address.houseNo", "%140%"));

                   List<Person> list = ctr.list();

                   for(Person p1 : list)

                   {

                             System.out.print(p1.getPersonId() + ",");

                             System.out.print(p1.getFirstName() + ",");

                             System.out.print(p1.getLastName() + ",");

                             System.out.print(p1.getAge() + ",");

                             System.out.print(p1.getAddress().getHouseNo() + ",");

                             System.out.println(p1.getAddress().getStreetName() + ",");

                   }

          }

}

package com.lara;

 

import java.util.List;

 

import org.hibernate.Query;

import org.hibernate.Session;

 

public class Manager10 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Query qry = s1.createQuery("from Person where lastName like '%c%'");

                   List<Person> list = qry.list();

                   for(Person p1 : list)

                   {

                             System.out.print(p1.getPersonId() + ",");

                             System.out.print(p1.getFirstName() + ",");

                             System.out.print(p1.getLastName() + ",");

                             System.out.print(p1.getAge() + ",");

                             System.out.print(p1.getAddress().getHouseNo() + ",");

                             System.out.println(p1.getAddress().getStreetName() + ",");                         

                   }

          }

}

package com.lara;

 

import java.util.List;

 

import org.hibernate.Query;

import org.hibernate.Session;

 

public class Manager11 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Query qry = s1.createQuery("from Person where address.streetName like '%Jp%'");

                   List<Person> list = qry.list();

                   for(Person p1 : list)

                   {

                             System.out.print(p1.getPersonId() + ",");

                             System.out.print(p1.getFirstName() + ",");

                             System.out.print(p1.getLastName() + ",");

                             System.out.print(p1.getAge() + ",");

                             System.out.print(p1.getAddress().getHouseNo() + ",");

                             System.out.println(p1.getAddress().getStreetName() + ",");                         

                   }

          }

}

package com.lara;

 

import java.util.Arrays;

import java.util.List;

 

import org.hibernate.Query;

import org.hibernate.Session;

 

public class Manager12 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Query qry = s1.createSQLQuery("select * from PERSON where street_name like '%Jp%'");

                   List<Object[]> list = qry.list();

                    for(Object[] record : list)

                   {

                             System.out.println(Arrays.toString(record));                     

                   }

          }

}

Person.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara">

 

    <class name="Person" table="PERSON">

        <id name="personId" column="PERSON_ID"/>

        <property name="firstName" column="first_name"/>

        <property name="lastName" column="last_name"/>

        <property name="age" column="person_age"/>

        <component name="address" class="Address">

          <property name="houseNo" column="house_no"/>

          <property name="streetName" column="street_name"/>

        </component>

    </class>

 

</hibernate-mapping>

 

package com.lara;

 

import java.io.Serializable;

 

public class StudentPK implements Serializable

{

          private String firstName;

          private Integer age;

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

}

package com.lara;

 

public class Student {

          private StudentPK studentPk;

          private String lastName;

          private String email;

         

          public StudentPK getStudentPk() {

                   return studentPk;

          }

          public void setStudentPk(StudentPK studentPk) {

                   this.studentPk = studentPk;

          }

          public String getLastName() {

                   return lastName;

          }

          public void setLastName(String lastName) {

                   this.lastName = lastName;

          }

          public String getEmail() {

                   return email;

          }

          public void setEmail(String email) {

                   this.email = email;

          }

         

}

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara">

 

    <class name="Student" table="STUDENT">

        <composite-id name="studentPk" class="StudentPK">

          <key-property name="firstName" column="first_name"/>

          <key-property name="age"/>

        </composite-id>      

        <property name="lastName" column="last_name"/>

        <property name="email"/>

    </class>

 

</hibernate-mapping>

package com.lara;

 

import org.hibernate.Session;

 

public class Manager13 {

          public static void main(String[] args) {

                   StudentPK pk = new StudentPK();

                   pk.setFirstName("Vijay");

                   pk.setAge(22);

                  

                   Student st = new Student();

                   st.setStudentPk(pk);

                   st.setLastName("B");

                   st.setEmail("v@gmail.com");

                  

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   s1.save(st);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

          }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager14 {

          public static void main(String[] args) {

                   StudentPK pk = new StudentPK();

                   pk.setFirstName("Ramu");

                   pk.setAge(22);             

                   Student st = new Student();

                   st.setStudentPk(pk);

                   st.setLastName("r");

                   st.setEmail("r@gmail.com");

 

                   StudentPK pk1 = new StudentPK();

                   pk1.setFirstName("Manu");

                   pk1.setAge(28);           

                   Student st1 = new Student();

                   st1.setStudentPk(pk1);

                   st1.setLastName("m");

                   st1.setEmail("r@hotmail.com");

                  

                  

                  

                  

                  

                  

                  

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   s1.save(st);

                   s1.save(st1);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

          }

}

 

package com.lara;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

 

public class Manager15 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Criteria ctr = s1.createCriteria(Student.class);

                   ctr.add(Restrictions.like("studentPk.firstName", "%R%"));

                   List<Student> list = ctr.list();         

                   for(Student st : list)

                   {

                             System.out.print(st.getStudentPk().getFirstName() + ",");

                             System.out.print(st.getStudentPk().getAge() + ",");

                             System.out.print(st.getLastName() + ",");

                             System.out.println(st.getEmail() + ",");

                   }

          }

}

package com.lara;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

 

public class Manager16 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Query qry = s1.createQuery("from Student where studentPk.firstName like '%R%'");

                   List<Student> list = qry.list();                  

                   for(Student st : list)

                   {

                             System.out.print(st.getStudentPk().getFirstName() + ",");

                             System.out.print(st.getStudentPk().getAge() + ",");

                             System.out.print(st.getLastName() + ",");

                             System.out.println(st.getEmail() + ",");

                   }

          }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager17 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                  

                   StudentPK pk = new StudentPK();

                   pk.setFirstName("Vijay");

                   pk.setAge(22);

                  

                   Student st = (Student) s1.get(Student.class, pk);

                  

                   System.out.println(st.getLastName());

                   System.out.println(st.getEmail());

                  

                  

          }

}

package com.lara.pack1;

 

public class Employee extends Person

{

          private Integer age;

          private String email;

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

          public String getEmail() {

                   return email;

          }

          public void setEmail(String email) {

                   this.email = email;

          }

         

}

package com.lara.pack1;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager1 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                  

                   Person p1 = new Person();

                   p1.setPersonId(101);

                   p1.setFirstName("Vijay");

                   p1.setLastName("b");

                  

                  

                   Employee e1 = new Employee();

                   e1.setPersonId(102);

                   e1.setFirstName("Ramu");

                   e1.setLastName("b");

                   e1.setAge(22);

                   e1.setEmail("r@r.com");

                  

                  

                   PermanentEmployee pe1 = new PermanentEmployee();

                   pe1.setPersonId(103);

                   pe1.setFirstName("Kiran");

                   pe1.setLastName("b");

                   pe1.setAge(25);

                   pe1.setEmail("k@r.com");

                   pe1.setSalary(50000.0);

                   pe1.setLeavesCount(22);

                  

                   s1.beginTransaction();

                   s1.save(p1);

                   s1.save(e1);

                   s1.save(pe1);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

          }

}

package com.lara.pack1;

 

public class PermanentEmployee extends Employee

{

          private Double salary;

          private Integer leavesCount;

          public Double getSalary() {

                   return salary;

          }

          public void setSalary(Double salary) {

                   this.salary = salary;

          }

          public Integer getLeavesCount() {

                   return leavesCount;

          }

          public void setLeavesCount(Integer leavesCount) {

                   this.leavesCount = leavesCount;

          }

         

         

}

package com.lara.pack1;

 

public class Person {

          private Integer personId;

          private String firstName;

          private String lastName;

          public Integer getPersonId() {

                   return personId;

          }

          public void setPersonId(Integer personId) {

                   this.personId = personId;

          }

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public String getLastName() {

                   return lastName;

          }

          public void setLastName(String lastName) {

                   this.lastName = lastName;

          }

         

}

Person1.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack1" auto-import="false">

    <class name="Person" table="PERSON">

        <id name="personId" column="PERSON_ID"/>

        <property name="firstName" column="first_name"/>

        <property name="lastName" column="last_name"/>

    </class>

    <class name="Employee" table="EMPLOYEE">

        <id name="personId" column="PERSON_ID"/>

        <property name="firstName" column="first_name"/>

        <property name="lastName" column="last_name"/>

        <property name="age"/>

        <property name="email"/>

    </class>

    <class name="PermanentEmployee" table="P_EMPLOYEE">

        <id name="personId" column="PERSON_ID"/>

        <property name="firstName" column="first_name"/>

        <property name="lastName" column="last_name"/>

        <property name="age"/>

        <property name="email"/>

        <property name="salary"/>

        <property name="leavesCount"/>

    </class>

</hibernate-mapping>

Person2.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack1" auto-import="false">

    <class name="Person" table="PERSON" discriminator-value="p">

        <id name="personId" column="PERSON_ID"/>

        <discriminator column="obj_type" type="string"/>

        <property name="firstName" column="first_name"/>

        <property name="lastName" column="last_name"/>

          <subclass name="Employee" discriminator-value="e">

                  <property name="age"/>

                  <property name="email"/>

                       <subclass name="PermanentEmployee" discriminator-value="pe">

                           <property name="salary"/>

                           <property name="leavesCount"/>

                       </subclass>

                   </subclass>

    </class>

</hibernate-mapping>

Person3.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack1" auto-import="false">

    <class name="Person" table="PERSON" discriminator-value="p">

        <id name="personId" column="PERSON_ID"/>

        <discriminator column="obj_type" type="string"/>

        <property name="firstName" column="first_name"/>

        <property name="lastName" column="last_name"/>

          <subclass name="Employee" discriminator-value="e">

                  <property name="age"/>

                  <property name="email"/>

                       <subclass name="PermanentEmployee" discriminator-value="pe">

                           <property name="salary"/>

                           <property name="leavesCount"/>

                       </subclass>

                   </subclass>

    </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.dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">great123</property>

        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

        <property name="hibernate.hbm2ddl.auto">create</property>

        <property name="hibernate.show_sql">true</property>

        <mapping resource="com/lara/pack1/Person3.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

 

test1.txt

102, vijay, b, 22, 140-B, BTM

103, kiran, c, 24, 140-B, BTM

104, manu, b, 42, 140-C, Jp Nagar

105, bhanu, c, 32, 140-B, BTM

106, bharath, b, 24, 140-B, Jp Nagar

107, murali, b, 25, 144-C, BTM

108, ramana, c, 72, 140-B, Jp Nagar

109, ravali, b, 28, 150-B, BTM

110, rajesh, c, 82, 144-B, BTM

111, kumar, b, 25, 140-B, Jp Nagar

112, houshik, c, 52, 144-B, BTM

113, pavan, b, 23, 140-B, BTM

114, mohan, c, 32, 140-C, Jp Nagar

 Sunday(11-12-2016)                 app6

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Util

{

            private static SessionFactory sf;

           

            static

            {

                        Configuration c1 = new Configuration();

                        c1.configure();

                        sf = c1.buildSessionFactory();               

            }

            public static Session getSession()

            {

                        return sf.openSession();

            }

}

package com.lara;

 

public class Person

{

            private Integer personId;

            private String firstName;

            private String lastName;

            private Integer age;

            public Integer getPersonId() {

                        return personId;

            }

            public void setPersonId(Integer personId) {

                        this.personId = personId;

            }

            public String getFirstName() {

                        return firstName;

            }

            public void setFirstName(String firstName) {

                        this.firstName = firstName;

            }

            public String getLastName() {

                        return lastName;

            }

            public void setLastName(String lastName) {

                        this.lastName = lastName;

            }

            public Integer getAge() {

                        return age;

            }

            public void setAge(Integer age) {

                        this.age = age;

            }

           

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager1 {

            public static void main(String[] args) {

                        Person p1 = new Person();

                        p1.setPersonId(101);

                        p1.setFirstName("Vijay");

                        p1.setLastName("abc");

                        p1.setAge(22);

                       

                        Session s1 = Util.getSession();

                        s1.beginTransaction();

                        s1.save(p1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

            }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager2 {

            public static void main(String[] args) {

                        Person p1 = new Person();

                        p1.setPersonId(102);

                        p1.setFirstName("Kiran");

                        p1.setLastName("abc");

                        p1.setAge(22);

                       

                        Session s1 = Util.getSession();

                        s1.beginTransaction();

                        s1.save(p1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

            }

}

Person.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara">

 

    <class name="Person" table="PERSON">

        <id name="personId" column="PERSON_ID"/>

        <property name="firstName" column="first_name" not-null="true"/>

        <property name="lastName" column="last_name" unique-key="puk1"/>

        <property name="age" column="person_age" unique-key="puk1"/>

    </class>

 

</hibernate-mapping>

package com.lara;

import java.util.Collection;

import java.util.List;

import java.util.Map;

import java.util.Set;

import javax.persistence.CollectionTable;

import javax.persistence.Column;

import javax.persistence.ElementCollection;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.JoinTable;

import javax.persistence.MapKey;

import javax.persistence.OneToMany;

import javax.persistence.OneToOne;

import javax.persistence.Table;

import javax.persistence.JoinColumn;;

public class Student

{

            private int id;

            private String name;

            private int[] mobileNos;

            private String[] projects;

            private Collection skills;

            private List marks;

            private Set jobs;

            private Map educations;

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getName() {

                        return name;

            }

            public void setName(String name) {

                        this.name = name;

            }

            public int[] getMobileNos() {

                        return mobileNos;

            }

            public void setMobileNos(int[] mobileNos) {

                        this.mobileNos = mobileNos;

            }

            public String[] getProjects() {

                        return projects;

            }

            public void setProjects(String[] projects) {

                        this.projects = projects;

            }

            public Collection getSkills() {

                        return skills;

            }

            public void setSkills(Collection skills) {

                        this.skills = skills;

            }

            public List getMarks() {

                        return marks;

            }

            public void setMarks(List marks) {

                        this.marks = marks;

            }

            public Set getJobs() {

                        return jobs;

            }

            public void setJobs(Set jobs) {

                        this.jobs = jobs;

            }

            public Map getEducations() {

                        return educations;

            }

            public void setEducations(Map educations) {

                        this.educations = educations;

            }

           

 

 

}

package com.lara;

 

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

 

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

public class Manager3

{

            public static void main(String[] args)

            {

                        Session s1 = Util.getSession();

                        Student st = new Student();

                        st.setId(101);

                        st.setName("abc");

                        st.setMobileNos(new int[]{984501235, 984501234});

                        String[] projects ={ "citi", "hdfc", "kotak" };

                        st.setProjects(projects);

                        Collection list = new ArrayList();

                        list.add("C");

                        list.add("C++");

                        list.add("Java");

                        st.setSkills(list);

                        List marks = new ArrayList();

                        marks.add(100);

                        marks.add(200);

                        marks.add(300);

                        st.setMarks(marks);

                        Set jobs = new HashSet();

                        jobs.add("software");

                        jobs.add("hardware");

                        jobs.add("teacher");

                        st.setJobs(jobs);

                        Map educations = new HashMap();

                        educations.put("degree", "B.Se");

                        educations.put("post-degree", "M.Sc");

                        st.setEducations(educations);

                        Transaction t = s1.beginTransaction();

                        s1.save(st);

                        t.commit();

                        System.out.println("done");

            }

}

package com.lara;

 

import java.util.Arrays;

 

import org.hibernate.Session;

 

public class Manager4 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st1 = (Student) s1.load(Student.class, 101);

                        System.out.println("-----");

                        System.out.println(st1.getName());

                        System.out.println(Arrays.toString(st1.getMobileNos()));

                        System.out.println(Arrays.toString(st1.getProjects()));

                        System.out.println(st1.getMarks());

                        System.out.println(st1.getSkills());

                        System.out.println(st1.getJobs());

                        System.out.println(st1.getEducations());

                       

            }

}

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara">

            <class name="Student" table="STUDENT">

                        <id name="id" column="s_id"/>

                        <property name="name" />

                        <primitive-array name="mobileNos" table="mobile_nos">

                                    <key column="S_ID"/>

                                    <list-index column="sno"/>

                                    <element column="m_no" type="int"/>

                        </primitive-array>

                        <array name="projects" table="PROJECTS">

                                    <key column="S_ID" />

                                    <list-index column="sno" />

                                    <element column="project_name" type="string" />

                        </array>

                        <bag name="skills" table="SKILLS" lazy="false">

                                    <key column="S_ID" />

                                    <element column="skills_name" type="string" />

                        </bag>

                        <list name="marks" table="MARKS">

                                    <key column="S_ID" />

                                    <list-index column="sno" />

                                    <element column="marks" type="int" />

                        </list>

                        <set name="jobs" table="JOBS">

                                    <key column="S_ID" />

                                    <element column="job" type="string" />

                        </set>

                        <map name="educations" table="EDUCATIONS">

                                    <key column="S_ID" />

                                    <map-key column="name" type="string" />

                                    <element column="education" type="string" />

                        </map>

            </class>

</hibernate-mapping>

package com.lara;

 

import java.util.Arrays;

 

import org.hibernate.Session;

 

public class Manager5 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st1 = (Student) s1.load(Student.class, 101);

                       

                        st1.setMobileNos(new int[]{123456, 567890});

                       

                        st1.setMarks(Arrays.asList(45, 35));

                       

                        s1.beginTransaction();

                        s1.update(st1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

            }

}

package com.lara;

 

import java.util.Arrays;

 

import org.hibernate.Session;

 

public class Manager6 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st1 = (Student) s1.load(Student.class, 101);

                        s1.beginTransaction();

                        s1.delete(st1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

            }

}

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.dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">great123</property>

        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.show_sql">true</property>

        <mapping resource="com/lara/Person.hbm.xml"/>

        <mapping resource="com/lara/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

 

 Saturday(17-12-2016)                            app7

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Util

{

          private static SessionFactory sf;

         

          static

          {

                   Configuration c1 = new Configuration();

                   c1.configure();

                   sf = c1.buildSessionFactory();                 

          }

          public static Session getSession()

          {

                   return sf.openSession();

          }

}

package com.lara;

 

public class Person

{

          private Integer personId;

          private String firstName;

          private Integer age;

          public Integer getPersonId() {

                   return personId;

          }

          public void setPersonId(Integer personId) {

                   this.personId = personId;

          }

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

         

         

         

}

package com.lara;

 

import java.io.Serializable;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

public class Manager1

{

          public static void main(String[] args) {

                   Person p1 = new Person();

                   p1.setPersonId(101);

                   p1.setFirstName("abc");

                   p1.setAge(22);

                  

                   Session s1 = Util.getSession();

                   Transaction tx = s1.beginTransaction();

                   Serializable ser = s1.save(p1);

                   System.out.println("saved with ser as:" + ser);

                   tx.commit();

                   System.out.println("commited tx");

                  

          }

}

package com.lara;

 

import java.io.Serializable;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

public class Manager2 {

          public static void main(String[] args) {

                   Person p1 = new Person();

                   p1.setPersonId(302);

                   p1.setFirstName("xyz");

                   p1.setAge(25);             

                   Session s1 = Util.getSession();

                   Transaction tx = s1.beginTransaction();

                   Serializable ser = s1.save(p1);

                   System.out.println("saved with ser as:" + ser);

                   tx.commit();

                   System.out.println("commited tx");                

          }

}

package com.lara;

 

import java.io.Serializable;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

public class Manager3 {

          public static void main(String[] args) {

                   Person p1 = new Person();

                   p1.setPersonId(3501);

                   p1.setFirstName("Vijay");

                   p1.setAge(28);             

                   Session s1 = Util.getSession();

                   Transaction tx = s1.beginTransaction();

                   Serializable ser = s1.save(p1);

                   System.out.println("saved with ser as:" + ser);

                   tx.commit();

                   System.out.println("commited tx");                

          }

}

package com.lara;

 

public class Student {

          private Integer studentId;

          private String firstName;

          private Integer age;

          public Integer getStudentId() {

                   return studentId;

          }

          public void setStudentId(Integer studentId) {

                   this.studentId = studentId;

          }

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

         

         

         

}

package com.lara;

 

import java.io.Serializable;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

public class Manager4 {

          public static void main(String[] args) {

                   Student st1 = new Student();

                   st1.setStudentId(101);

                   st1.setFirstName("Vijay");

                   st1.setAge(28);            

                   Session s1 = Util.getSession();

                   Transaction tx = s1.beginTransaction();

                   Serializable ser = s1.save(st1);

                   System.out.println("saved with ser as:" + ser);

                   tx.commit();

                   System.out.println("commited tx");                

          }

}

Person.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara">

    <class name="Person" table="PERSON">

        <id name="personId" column="person_id"/>

        <property name="firstName" column="first_name"/>

        <property name="age"/>

    </class>

</hibernate-mapping>

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara">

    <class name="Student" table="STUDENT">

        <id name="studentId" column="student_id"/>

        <property name="firstName" column="first_name"/>

        <property name="age"/>

    </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.dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">great123</property>

        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.show_sql">true</property>       

        <mapping resource="com/lara/Person.hbm.xml"/>

        <mapping resource="com/lara/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

app8

pack1

package com.lara.pack1;

 

public class Student

{

          private Integer studentId;

          private String firstName;

          private Integer age;

         

          public Integer getStudentId() {

                   return studentId;

          }

          public void setStudentId(Integer studentId) {

                   this.studentId = studentId;

          }

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

         

         

         

}

package com.lara.pack1;

 

public class Address {

          private Integer addressId;

          private String houseNo;

          private String streetName;

          private Student student;

         

          public Integer getAddressId() {

                   return addressId;

          }

          public void setAddressId(Integer addressId) {

                   this.addressId = addressId;

          }

          public String getHouseNo() {

                   return houseNo;

          }

          public void setHouseNo(String houseNo) {

                   this.houseNo = houseNo;

          }

          public String getStreetName() {

                   return streetName;

          }

          public void setStreetName(String streetName) {

                   this.streetName = streetName;

          }

          public Student getStudent() {

                   return student;

          }

          public void setStudent(Student student) {

                   this.student = student;

          }

}

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack1">

    <class name="Student" table="STUDENT">

        <id name="studentId" column="student_id"/>

        <property name="firstName" column="first_name"/>

        <property name="age"/>

    </class>

</hibernate-mapping>

 

 

package com.lara.pack1;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager2 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Address add = (Address) s1.get(Address.class, 101);

                   System.out.println(add.getHouseNo());

                   System.out.println(add.getStreetName());

                   Student st1 = add.getStudent();

                   System.out.println(st1.getFirstName());

                   System.out.println(st1.getAge());           

          }

}

package com.lara.pack1;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager3 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                    Address add = (Address) s1.get(Address.class, 101);

                   add.setHouseNo("520/T");            

                   s1.beginTransaction();

                   s1.update(add);

                   s1.getTransaction().commit();                

                   System.out.println("done");          

          }       

}

package com.lara.pack1;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager5 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Address add = (Address) s1.get(Address.class, 101);

                   s1.beginTransaction();

                   s1.delete(add);

                   s1.getTransaction().commit();                

                   System.out.println("done");          

          }       

}

package com.lara.pack1;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager1 {

          public static void main(String[] args) {

                   Student st1 = new Student();

                   st1.setStudentId(101);

                   st1.setFirstName("xyz");

                   st1.setAge(22);

                  

                   Address add = new Address();

                   add.setHouseNo("130/B");

                   add.setStreetName("BTM");

                   add.setStudent(st1);

                  

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   //s1.save(st1);

                   s1.persist(add);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

                  

                  

                  

          }

}

package com.lara.pack1;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager4 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Address add = (Address) s1.get(Address.class, 101);

                   add.getStudent().setFirstName("MANOHAR");                 

                   s1.beginTransaction();

                   s1.update(add);

                   s1.getTransaction().commit();                

                   System.out.println("done");          

          }       

}

Address.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack1">

    <class name="Address" table="ADDRESS">

        <id name="addressId" column="address_id">

          <generator class="foreign">

                   <param name="property">student</param>

          </generator>

        </id>

        <property name="houseNo" column="house_no"/>

        <property name="streetName" column="street_name"/>

        <one-to-one name="student"

                             class="Student"

                             constrained="true"

                             cascade="delete" />

                                                <!--

                                                          1. persist

                                                          2. save-update

                                                          3. delete

                                                          4. none  (default)

                                                          5. all

                                                -->                                 

    </class>

</hibernate-mapping>

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Util

{

          private static SessionFactory sf;

         

          static

          {

                   Configuration c1 = new Configuration();

                   c1.configure();

                   sf = c1.buildSessionFactory();                 

          }

          public static Session getSession()

          {

                   return sf.openSession();

          }

}

pack2

package com.lara.pack2;

 

public class Student

{

          private Integer studentId;

          private String firstName;

          private Integer age;

          private Address address;

          public Integer getStudentId() {

                   return studentId;

          }

          public void setStudentId(Integer studentId) {

                   this.studentId = studentId;

          }

          public String getFirstName() {

                   return firstName;

          }

          public void setFirstName(String firstName) {

                   this.firstName = firstName;

          }

          public Integer getAge() {

                   return age;

          }

          public void setAge(Integer age) {

                   this.age = age;

          }

          public Address getAddress() {

                   return address;

          }

          public void setAddress(Address address) {

                   this.address = address;

          }

         

         

         

}

package com.lara.pack2;

 

public class Address {

          private Integer addressId;

          private String houseNo;

          private String streetName;

          private Student student;

          public Integer getAddressId() {

                   return addressId;

          }

          public void setAddressId(Integer addressId) {

                   this.addressId = addressId;

          }

          public String getHouseNo() {

                   return houseNo;

          }

          public void setHouseNo(String houseNo) {

                   this.houseNo = houseNo;

          }

          public String getStreetName() {

                   return streetName;

          }

          public void setStreetName(String streetName) {

                   this.streetName = streetName;

          }

          public Student getStudent() {

                   return student;

          }

          public void setStudent(Student student) {

                   this.student = student;

          }

         

         

}

Address.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack2">

    <class name="Address" table="ADDRESS">

        <id name="addressId" column="address_id">

          <generator class="foreign">

                   <param name="property">student</param>

          </generator>

        </id>

        <property name="houseNo" column="house_no"/>

        <property name="streetName" column="street_name"/>

        <one-to-one name="student"

                             class="Student"

                             constrained="true"

                             cascade="all" />          

    </class>

</hibernate-mapping>

 

package com.lara.pack2;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager1 {

          public static void main(String[] args) {

                   Student st1 = new Student();

                   st1.setStudentId(101);

                   st1.setFirstName("abc");

                   st1.setAge(22);

                  

                   Address add = new Address();

                   add.setHouseNo("150/Y");

                   add.setStreetName("BTM");

                  

                   st1.setAddress(add);

                   add.setStudent(st1);

                  

                   Session s1 = Util.getSession();

                   s1.beginTransaction();

                   s1.persist(st1);

                   s1.getTransaction().commit();

                  

                  

          }

}

package com.lara.pack2;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager2 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                   Student st1 = (Student) s1.get(Student.class, 101);

                   System.out.println(st1.getFirstName());

                   System.out.println(st1.getAge());

                   System.out.println(st1.getAddress().getHouseNo());

                   System.out.println(st1.getAddress().getStreetName());

                  

          }

}

package com.lara.pack2;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager3 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();

                  

                   Student st1 = (Student) s1.get(Student.class, 101);

                   st1.getAddress().setHouseNo("809/U");

                  

                   s1.beginTransaction();

                   s1.update(st1);

                   s1.getTransaction().commit();

                  

                   System.out.println("done");

          }

}

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara.pack2">

    <class name="Student" table="STUDENT">

        <id name="studentId" column="student_id"/>

        <property name="firstName" column="first_name"/>

        <property name="age"/>

        <one-to-one name="address" class="Address" cascade="delete"/>

    </class>

</hibernate-mapping>

 

 

package com.lara.pack2;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager4 {

          public static void main(String[] args) {

                   Session s1 = Util.getSession();                

                   Student st1 = (Student) s1.get(Student.class, 101);          

                   s1.beginTransaction();

                   s1.delete(st1);

                   s1.getTransaction().commit();                

          }

}

 Sunday(18-12-2016)                              

pack3

package com.lara.pack3;

public class Student

{

            private int id;

            private String firstName;

            private String lastName;

           

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getFirstName() {

                        return firstName;

            }

            public void setFirstName(String firstName) {

                        this.firstName = firstName;

            }

            public String getLastName() {

                        return lastName;

            }

            public void setLastName(String lastName) {

                        this.lastName = lastName;

            }

}

package com.lara.pack3;

public class Mail

{

            private int id;

            private String username;

            private String password;

            private Student student;

 

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getUsername() {

                        return username;

            }

            public void setUsername(String username) {

                        this.username = username;

            }

            public String getPassword() {

                        return password;

            }

            public void setPassword(String password) {

                        this.password = password;

            }

            public void setStudent(Student student) {

                        this.student = student;

            }

            public Student getStudent() {

                        return student;

            }

           

}

package com.lara.pack3;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.lara.Util;

 

public class Manager1

{

            public static void main(String[] args)

            {

                        Session s1 = Util.getSession();

                       

                        Student st = new Student();

                        st.setFirstName("Ramu");

                        st.setLastName("Vijay");             

                        Mail m1 = new Mail();

                        m1.setUsername("abc");

                        m1.setPassword("xyz");

                        m1.setStudent(st);            

                        Mail m2 = new Mail();

                        m2.setUsername("hello");

                        m2.setPassword("test");

                        m2.setStudent(st);

                       

                       

                        Student st1 = new Student();

                        st1.setFirstName("Kiran");

                        st1.setLastName("Kumar");                   

                        Mail m3 = new Mail();

                        m3.setUsername("system");

                        m3.setPassword("great123");

                        m3.setStudent(st1);                     

                        Mail m4 = new Mail();

                        m4.setUsername("java");

                        m4.setPassword("blr");

                        m4.setStudent(st1);

                       

 

                        Transaction t = s1.beginTransaction();

                        //s1.save(st);

                        //s1.save(st1);

                        s1.save(m1);

                        s1.save(m2);

                        s1.save(m3);

                        s1.save(m4);

                        t.commit();

                        System.out.println("done");

 

            }

}

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara.pack3">

            <class name="Student" table="STUDENT">

                        <id name="id" column="S_ID">

                                    <generator class="sequence">

                                                <param name="sequence">student_seq</param>

                                    </generator>

                        </id>

                        <property name="firstName" />

                        <property name="lastName" />

            </class>

</hibernate-mapping>

Mail.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara.pack3"> 

            <class name="Mail" table="MAIL">

                        <id name="id" column="M_ID">

                                    <generator class="sequence">

                                                <param name="sequence">mail_seq</param>

                                    </generator>

                        </id>

                        <property name="username" />

                        <property name="password" />

                        <many-to-one name="student"

                                                             class="Student"

                                                             column="student_id"

                                                             cascade="all"/>

            </class>

</hibernate-mapping>

package com.lara.pack3;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager2 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Mail m1 = (Mail) s1.load(Mail.class, 3);

                        System.out.println(m1.getUsername());

                        System.out.println(m1.getPassword());

                        System.out.println(m1.getStudent().getFirstName());

                        System.out.println(m1.getStudent().getLastName());

            }

}

package com.lara.pack3;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager3 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 1);

                        System.out.println(st.getFirstName());

                        System.out.println(st.getLastName());

            }

}

package com.lara.pack3;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager4 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Mail m1 = (Mail) s1.get(Mail.class, 2);

                        m1.setPassword("1243");

                        m1.getStudent().setLastName("test");

                       

                        s1.beginTransaction();

                        s1.update(m1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

                       

                       

            }

}

package com.lara.pack3;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager5 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Mail m1 = (Mail) s1.load(Mail.class, 4);                      

                        s1.beginTransaction();

                        s1.delete(m1);

                        s1.getTransaction().commit();              

                        System.out.println("done");                  

            }

}

package com.lara.pack3;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager6 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Mail m1 = (Mail) s1.load(Mail.class, 4);                      

                        Mail m2 = (Mail) s1.load(Mail.class, 3);

                        s1.beginTransaction();

                        s1.delete(m1);

                        s1.delete(m2);

                        s1.getTransaction().commit();              

                        System.out.println("done");                  

            }

}

pack4

package com.lara.pack4;

public class Student

{

            private int id;

            private String firstName;

            private String lastName;

            private Mail[] mails;

 

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getFirstName() {

                        return firstName;

            }

            public void setFirstName(String firstName) {

                        this.firstName = firstName;

            }

            public String getLastName() {

                        return lastName;

            }

            public void setLastName(String lastName) {

                        this.lastName = lastName;

            }

            public void setMails(Mail[] mails) {

                        this.mails = mails;

            }

            public Mail[] getMails() {

                        return mails;

            }

}

package com.lara.pack4;

 

public class Mail

{

            private int id;

            private String username;

            private String password;

            private Student student;

           

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getUsername() {

                        return username;

            }

            public void setUsername(String username) {

                        this.username = username;

            }

            public String getPassword() {

                        return password;

            }

            public void setPassword(String password) {

                        this.password = password;

            }

            public void setStudent(Student student) {

                        this.student = student;

            }

            public Student getStudent() {

                        return student;

            }

           

}

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara.pack4">

            <class name="Student" table="STUDENT">

                        <id name="id" column="S_ID">

                                    <generator class="sequence">

                                                <param name="sequence">student_seq</param>

                                    </generator>

                        </id>

                        <property name="firstName" />

                        <property name="lastName" />

                        <array name="mails" cascade="all">

            <key column="student_id"/>

            <index column="sno"/>

            <one-to-many class="Mail"/>               

            </array>

            </class>

</hibernate-mapping>

 

 

Mail.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara.pack4"> 

            <class name="Mail" table="MAIL">

                        <id name="id" column="M_ID">

                                    <generator class="sequence">

                                                <param name="sequence">mail_seq</param>

                                    </generator>

                        </id>

                        <property name="username" />

                        <property name="password" />

                        <many-to-one name="student"

                                                             class="Student"

                                                             column="student_id"

                                                             cascade="all"/>

            </class>

</hibernate-mapping>

package com.lara.pack4;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.lara.Util;

 

public class Manager1

{

            public static void main(String[] args)

            {

                        Session s1 = Util.getSession();

                       

                        Student st = new Student();

                        st.setFirstName("Ramu");

                        st.setLastName("Vijay");             

                        Mail m1 = new Mail();

                        m1.setUsername("abc");

                        m1.setPassword("xyz");

                        m1.setStudent(st);            

                        Mail m2 = new Mail();

                        m2.setUsername("hello");

                        m2.setPassword("test");

                        m2.setStudent(st);            

                        st.setMails(new Mail[]{m1, m2});

                       

                        Student st1 = new Student();

                        st1.setFirstName("Kiran");

                        st1.setLastName("Kumar");                   

                        Mail m3 = new Mail();

                        m3.setUsername("123");

                        m3.setPassword("xyz");

                        m3.setStudent(st1);                     

                        Mail m4 = new Mail();

                        m4.setUsername("sys");

                        m4.setPassword("abc");

                        m4.setStudent(st1);                     

                        st1.setMails(new Mail[]{m3, m4});

                        Transaction t = s1.beginTransaction();

                        s1.save(st);

                        s1.save(st1);

                        //s1.persist(m1);

                        //s1.persist(m2);

                        t.commit();

 

                        System.out.println("done");

            }

}

package com.lara.pack4;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager2 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 2);

                        System.out.println(st.getFirstName());

                        System.out.println(st.getLastName());

                        for(Mail mail : st.getMails())

                        {

                                    System.out.println(mail.getUsername());

                                    System.out.println(mail.getPassword());

                        }

            }

}

package com.lara.pack4;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager3 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 1);

                        st.setLastName("bhanu");

                        st.getMails()[0].setUsername("usd");

                       

                        s1.beginTransaction();

                        s1.update(st);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

                       

            }

}

package com.lara.pack4;

 

import com.lara.Util;

 

import org.hibernate.Session;

 

public class Manager4 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 1);

                       

                        s1.beginTransaction();

                        s1.delete(st);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

            }

}

package com.lara.pack4;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager5 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Mail m1 = (Mail) s1.get(Mail.class, 4);

                       

                        s1.beginTransaction();

                        s1.delete(m1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

            }

}

pack5

package com.lara.pack5;

 

import java.util.HashSet;

import java.util.Set;

 

public class Student

{

            private int id;

            private String firstName;

            private String lastName;

            private Set batches = new HashSet();

           

           

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getFirstName() {

                        return firstName;

            }

            public void setFirstName(String firstName) {

                        this.firstName = firstName;

            }

            public String getLastName() {

                        return lastName;

            }

            public void setLastName(String lastName) {

                        this.lastName = lastName;

            }

            public void setBatches(Set batches) {

                        this.batches = batches;

            }

            public Set getBatches() {

                        return batches;

            }

}

package com.lara.pack5;

 

import java.util.HashSet;

import java.util.Set;

 

public class Batch

{

            private int id;

            private String title;

            private int duration;

            private Set students = new HashSet();

           

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getTitle() {

                        return title;

            }

            public void setTitle(String title) {

                        this.title = title;

            }

            public int getDuration() {

                        return duration;

            }

            public void setDuration(int duration) {

                        this.duration = duration;

            }

            public Set getStudents() {

                        return students;

            }

            public void setStudents(Set students) {

                        this.students = students;

            }

           

            public void addStudent(Student std)

            {

                        students.add(std);

                        std.getBatches().add(this);

            }

           

}

package com.lara.pack5;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.lara.Util;

 

public class Manager1

{

            public static void main(String[] args)

            {

                        Session s1 = Util.getSession();

                        Student st = new Student();

                        st.setFirstName("Ramu");

                        st.setLastName("Vijay");

                       

                        Student st1 = new Student();

                        st1.setFirstName("Kiran");

                        st1.setLastName("Vijay");

                       

                        Batch b1 = new Batch();

                        b1.setTitle("regular");

                        b1.setDuration(200);

                       

                        Batch b2 = new Batch();

                        b2.setTitle("crash");

                        b2.setDuration(100);

 

                        b1.addStudent(st);

                        b1.addStudent(st1);

                        b2.addStudent(st);

                        b2.addStudent(st1);

                       

                        Transaction t = s1.beginTransaction();

                        s1.save(st1);

                        s1.save(st);

                        s1.save(b1);

                        s1.save(b2);

                        t.commit();

                        System.out.println("done");

            }

}

package com.lara.pack5;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager2 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 2);

                        System.out.println(st.getFirstName());

                        System.out.println(st.getLastName());

                        Batch b1;

                        for(Object obj : st.getBatches())

                        {

                                    b1 = (Batch) obj;

                                    System.out.print(b1.getTitle() + ":");

                                    System.out.println(b1.getDuration());

                        }

            }

}

package com.lara.pack5;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager3 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Batch b1 = (Batch) s1.load(Batch.class, 1);

                        System.out.println(b1.getTitle());

                        System.out.println(b1.getDuration());

                        Student st;

                        for(Object obj : b1.getStudents())

                        {

                                    st = (Student) obj;

                                    System.out.println(st.getFirstName() + ":" + st.getLastName());

                        }

            }

}

package com.lara.pack5;

 

import java.util.ArrayList;

import java.util.List;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager4 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Batch b1 = (Batch) s1.get(Batch.class, 2);

                       

                        b1.setDuration(500);

                        List list = new ArrayList(b1.getStudents());

                        ((Student)list.get(0)).setLastName("KUMAR");

                       

                        s1.beginTransaction();

                        s1.update(b1);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

            }

}

 

package com.lara.pack5;

 

import java.util.ArrayList;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager5 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 1);

                       

                        st.setFirstName("ABC");

                       

                        ((Batch)(new ArrayList(st.getBatches()).get(0))).setTitle("new batch");

                       

                       

                        s1.beginTransaction();

                        s1.update(st);

                        s1.getTransaction().commit();

                       

                        System.out.println("done");

                       

            }

}

package com.lara.pack5;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager6 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Student st = (Student) s1.get(Student.class, 2);                   

                        s1.beginTransaction();

                        s1.delete(st);

                        s1.getTransaction().commit();              

                        System.out.println("done");

            }

}

package com.lara.pack5;

 

import org.hibernate.Session;

 

import com.lara.Util;

 

public class Manager7 {

            public static void main(String[] args) {

                        Session s1 = Util.getSession();

                        Batch b1 = (Batch) s1.get(Batch.class, 1);                  

                        s1.beginTransaction();

                        s1.delete(b1);

                        s1.getTransaction().commit();              

                        System.out.println("done");

            }

 

}

Batch.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara.pack5"> 

            <class name="Batch" table="Batch">

                        <id name="id" column="B_ID">

                                    <generator class="sequence">

                                                <param name="sequence">batch_seq</param>

                                    </generator>

                        </id>

                        <property name="title" />

                        <property name="duration" />

                        <set name="students" table="s_b" inverse="true">

            <key column="b_id"/>

            <many-to-many column="s_id" class="Student"/>           

            </set>

            </class>

</hibernate-mapping>

Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!-- -->

 

<hibernate-mapping package="com.lara.pack5">

            <class name="Student" table="STUDENT">

                        <id name="id" column="S_ID">

                                    <generator class="sequence">

                                                <param name="sequence">student_seq</param>

                                    </generator>

                        </id>

                        <property name="firstName" />

                        <property name="lastName" />

                        <set name="batches" table="s_b">

            <key column="s_id"/>

            <many-to-many column="b_id" class="Batch"/>               

            </set>

            </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.dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">great123</property>

        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.show_sql">true</property>       

        <mapping resource="com/lara/pack5/Student.hbm.xml"/>

        <mapping resource="com/lara/pack5/Batch.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

 

 Saturday(31-12-2016)       Hibernate-annotation

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.NamedNativeQueries;

import javax.persistence.NamedNativeQuery;

import javax.persistence.Table;

 

@Entity

@Table(name="person")

 

@NamedNativeQueries

({

@NamedNativeQuery(name="q1",

                                                  query="select * from person"),

@NamedNativeQuery(name="q2",

            query="select * from person where age < 50"),                                        

@NamedNativeQuery(name="q3",

            query="select * from person where age < ?"),                              

@NamedNativeQuery(name="q4",

query="select * from person where age < :abc"),                                                 

@NamedNativeQuery(name="q5",

query="select * from person", resultClass=Person.class)                                                

})         

public class Person

{

            @Id

            private int personId;

           

            @Column(name="first_name")

            private String firstName;

           

            private String lastName;

            private int age;

            //setters and getters

}

<mapping class="com.lara.Person"/>

import javax.persistence.Column;

import javax.persistence.Embeddable;

 

@Embeddable

public class Address

{

            @Column(name="house_no")

            private String houseNo;

           

            @Column(name="street_name")

            private String streetName;

           

            private String city;

            private String state;

//setters and getters

}

import javax.persistence.Column;

import javax.persistence.Embedded;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.NamedNativeQueries;

import javax.persistence.NamedNativeQuery;

import javax.persistence.Table;

 

@Entity

@Table(name="person")

public class Person

{

            @Id

            @Column(name="person_id")

            private String personId;

           

            @Column(name="first_name")

            private String firstName;

           

            @Column(name="last_name")

            private String lastName;

           

            private int age;

           

            @Embedded

            private Address address;

//stters and getters

}

@Embeddable

public class PersonPK implements Serializable

{

            @Column(name="first_name")

            private String firstName;

            private String email;

}

import javax.persistence.Column;

import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

import javax.persistence.NamedNativeQueries;

import javax.persistence.NamedNativeQuery;

import javax.persistence.Table;

 

@Entity

@Table(name="person")

@NamedNativeQueries({

@NamedNativeQuery(name="q1", query="select * from person"),

@NamedNativeQuery(name="q2", query="select * from person where age > 24"),

})

public class Person

{

            @EmbeddedId

            private PersonPK personPk;

           

            @Column(name="last_name")

            private String lastName;

            private int age;

            private String address;

            }

import javax.persistence.DiscriminatorColumn;

import javax.persistence.DiscriminatorType;

import javax.persistence.DiscriminatorValue;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Inheritance;

import javax.persistence.InheritanceType;

import javax.persistence.Table;

 

@Entity

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

@Table(name = "PERSON")

public class Person

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

}

@Entity

@Table(name = "EMPLOYEE")

public class Employee extends Person

{

            private String email;

            private String address;

}

@Entity

@Table(name = "P_EMPLOYEE")

public class PermanentEmployee extends Employee

{

            private double salary;

            private String designation;

}

import javax.persistence.DiscriminatorColumn;

import javax.persistence.DiscriminatorType;

import javax.persistence.DiscriminatorValue;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Inheritance;

import javax.persistence.InheritanceType;

import javax.persistence.Table;

 

@Entity

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

@DiscriminatorColumn(name = "discriminator",

                                                             discriminatorType = DiscriminatorType.STRING)

@DiscriminatorValue(value = "p")

@Table(name = "PERSON")

public class Person

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

}

@Entity

@DiscriminatorValue(value = "e")

public class Employee extends Person

{

            private String email;

            private String address;

}

@Entity

@DiscriminatorValue(value = "pe")

public class PermanentEmployee extends Employee

{

            private double salary;

            private String designation;

}

import javax.persistence.DiscriminatorColumn;

import javax.persistence.DiscriminatorType;

import javax.persistence.DiscriminatorValue;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Inheritance;

import javax.persistence.InheritanceType;

import javax.persistence.Table;

 

@Entity

@Table(name = "PERSON")

@Inheritance(strategy = InheritanceType.JOINED)

public class Person

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

            }

] @Entity

@Table(name = "EMPLOYEE")

@PrimaryKeyJoinColumn(name = "P_ID")

public class Employee extends Person

{

            private String email;

            private String address;

}

import javax.persistence.DiscriminatorValue;

import javax.persistence.Entity;

import javax.persistence.PrimaryKeyJoinColumn;

import javax.persistence.Table;

 

@Entity

@Table(name = "P_EMPLOYEE")

@PrimaryKeyJoinColumn(name = "E_ID")

public class PermanentEmployee extends Employee

{

            private double salary;

            private String designation;

}

import java.util.Collection;

import java.util.List;

import java.util.Map;

import java.util.Set;

import javax.persistence.CollectionTable;

import javax.persistence.Column;

import javax.persistence.ElementCollection;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.MapKeyColumn;

import javax.persistence.Table;

import org.hibernate.annotations.IndexColumn;

@Entity

@Table(name="student")

public class Student

{

            @Id

            private int id;

            private String name;

           

            @ElementCollection

            @CollectionTable(name = "mobile_numbers",

                             joinColumns = @JoinColumn(name = "S_ID"))

            @IndexColumn(name="sno")

            @Column(name = "M_NUMBER")

            private int[] mobileNos;

 

            @ElementCollection

            @CollectionTable(name = "projects",

                             joinColumns = @JoinColumn(name = "S_ID"))

            @IndexColumn(name="sno")

            @Column(name = "P_NAME")

            private String[] projects;

           

            @ElementCollection

            @CollectionTable(name = "skills",

                             joinColumns = @JoinColumn(name = "S_ID"))

            @Column(name = "SKILL_NAME")

            private Collection<String> skills;

 

            @ElementCollection

            @CollectionTable(name = "marks",

                             joinColumns = @JoinColumn(name = "S_ID"))

            @IndexColumn(name="sno")

            @Column(name = "MARKS")

            private List<Integer> marks;

           

            @ElementCollection

            @CollectionTable(name = "jobs",

                             joinColumns = @JoinColumn(name = "S_ID"))

            @Column(name = "job_name")

            private Set<String> jobs;

           

            @ElementCollection

            @CollectionTable(name = "educations",

                                                            joinColumns = @JoinColumn(name = "S_ID"))

            @MapKeyColumn(name="education_type")

            @Column(name = "education_name")

            private Map<String, String> educations;

           

}

import javax.persistence.CascadeType;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.OneToOne;

import javax.persistence.PrimaryKeyJoinColumn;

import javax.persistence.Table;

 

import org.hibernate.annotations.GenericGenerator;

import org.hibernate.annotations.Parameter;

 

@Entity

@Table(name="address")

@GenericGenerator(name="gen",

                                                  strategy="foreign",

                                                  parameters=@Parameter(name="property",

                                                                                                                value="student"))

public class Address

{

            @Id

            @GeneratedValue(generator="gen")

            private int addressId;

            private String houseNo;

            private String streetName;

            @OneToOne(cascade=CascadeType.ALL)

            //@PrimaryKeyJoinColumn

            private Student student;

}

import javax.persistence.CascadeType;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.OneToOne;

import javax.persistence.Table;

 

@Entity

@Table(name="student")

public class Student

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

}

@Entity

@Table(name="student")

public class Student

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

}

@Entity

@Table(name="mails")

public class Mail

{

            @Id

            private int id;

            private String username;

            private String password;

            @ManyToOne(cascade=CascadeType.ALL)

            @JoinColumn(name = "STD_ID", nullable = false)

            private Student student;

}

@Entity

@Table(name="student")

public class Student

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

            @OneToMany(cascade=CascadeType.ALL)

            @JoinColumn(name = "student_id")

            @IndexColumn(name="sno")

            private Mail[] mails;

}

@Entity

@Table(name="mails")

public class Mail

{

            @Id

            private int id;

            private String username;

            private String password;

            @ManyToOne

            private Student student;

}

@Entity

@Table(name="batch")

public class Batch

{

            @Id

            private int id;

            private String title;

            @ManyToMany(mappedBy="batches")

            private Set<Student> students = new HashSet<Student>();

}

@Entity

@Table(name="student")

public class Student

{

            @Id

            private int id;

            private String firstName;

            private String lastName;

            @ManyToMany(cascade = {CascadeType.ALL})

    @JoinTable(name="B_S",

                joinColumns={@JoinColumn(name="S_ID")},

                inverseJoinColumns={@JoinColumn(name="B_ID")}) 

            private Set<Batch> batches = new HashSet<Batch>();

}

Saturday(07-01-2017)                          app9

package com.lara;

public class Person

{

            private Integer id;

            private String firstName;

            private String lastName;

            public Integer getId() {

                        return id;

            }

            public void setId(Integer id) {

                        this.id = id;

            }

            public String getFirstName() {

                        return firstName;

            }

            public void setFirstName(String firstName) {

                        this.firstName = firstName;

            }

            public String getLastName() {

                        return lastName;

            }

            public void setLastName(String lastName) {

                        this.lastName = lastName;

            }

                       

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager1

{

            public static void main(String[] args)

            {

                        Person p1 = new Person();

                        p1.setId(101);

                        p1.setFirstName("abc");

                        p1.setLastName("xyz");

                       

                       

                        Session session = Util.getSession();

                        session.beginTransaction();

                        session.save(p1);

                        session.getTransaction().commit();

                       

                        System.out.println("done");

            }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager2 {

            public static void main(String[] args) {

                        Session session1 = Util.getSession();

                        Session session2 = Util.getSession();

                       

                        Person p1 = (Person) session1.get(Person.class, 101);

                        Person p2 = (Person) session1.get(Person.class, 101);

                        Person p3 = (Person) session1.get(Person.class, 101);

                       

                        System.out.println("---------");

                       

                        Person p4 = (Person) session2.get(Person.class, 101);

                        Person p5 = (Person) session2.get(Person.class, 101);

                        Person p6 = (Person) session2.get(Person.class, 101);

                       

                       

            }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager3 {

            public static void main(String[] args) {

                        Session session1 = Util.getSession();

                        Session session2 = Util.getSession();

                       

                        Person p1 = (Person) session1.get(Person.class, 101);

                        session1.evict(p1);

                        Person p2 = (Person) session1.get(Person.class, 101);

                        session1.evict(p2);

                        Person p3 = (Person) session1.get(Person.class, 101);

                        session1.evict(p3);

                       

                        System.out.println("---------");

                       

                        Person p4 = (Person) session2.get(Person.class, 101);

                        session2.evict(p4);

                        Person p5 = (Person) session2.get(Person.class, 101);

                        session2.evict(p5);

                        Person p6 = (Person) session2.get(Person.class, 101);

                        session2.evict(p6);

                       

            }

}

package com.lara;

 

import org.hibernate.Session;

 

public class Manager4 {

            public static void main(String[] args) {

                        Session session1 = Util.getSession();

                        Session session2 = Util.getSession();

                       

                        Person p1 = (Person) session1.get(Person.class, 101);

                        session1.clear();

                        Person p2 = (Person) session1.get(Person.class, 101);

                        session1.clear();

                        Person p3 = (Person) session1.get(Person.class, 101);

                        session1.clear();

                       

                        System.out.println("---------");

                       

                        Person p4 = (Person) session2.get(Person.class, 101);

                        session2.clear();

                        Person p5 = (Person) session2.get(Person.class, 101);

                        session2.clear();

                        Person p6 = (Person) session2.get(Person.class, 101);

                        session2.clear();

                       

            }

}

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Util {

            static SessionFactory sf;

            static

            {

                        Configuration c1 = new Configuration();

                        c1.configure();

                        sf = c1.buildSessionFactory();

            }

            public static Session getSession()

            {

                        Session s1 = sf.openSession();

                        return s1;

            }

}

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Manager5 {

            public static void main(String[] args) {

                        Session session1 = Util.getSession();

                        Session session2 = Util.getSession();

                       

           

                        Person p1 = (Person) session1.load(Person.class, 101);

                        System.out.println(p1.getFirstName());

                        Person p2 = (Person) session1.load(Person.class, 101);

                        System.out.println(p1.getFirstName());

                        Person p3 = (Person) session1.load(Person.class, 101);

                        System.out.println(p1.getFirstName());

                       

                        System.out.println("---------");

                       

                        Person p4 = (Person) session2.load(Person.class, 101);

                        System.out.println(p1.getFirstName());

                        Person p5 = (Person) session2.load(Person.class, 101);

                        System.out.println(p1.getFirstName());

                        Person p6 = (Person) session2.load(Person.class, 101);

                        System.out.println(p1.getFirstName());

                       

                       

            }

}

person.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<!--

 

-->

 

<hibernate-mapping package="com.lara">

 

    <class name="Person" table="PERSON">

        <id name="id"/>

        <property name="firstName"/>

        <property name="lastName"/>

    </class>

 

</hibernate-mapping>

 

ehcache.xml

<!--

  ~ Hibernate, Relational Persistence for Idiomatic Java

  ~

  ~ Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as

  ~ indicated by the @author tags or express copyright attribution

  ~ statements applied by the authors. All third-party contributions are

  ~ distributed under license by Red Hat Middleware LLC.

  ~

  ~ This copyrighted material is made available to anyone wishing to use, modify,

  ~ copy, or redistribute it subject to the terms and conditions of the GNU

  ~ Lesser General Public License, as published by the Free Software Foundation.

  ~

  ~ This program is distributed in the hope that it will be useful,

  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY

  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License

  ~ for more details.

  ~

  ~ You should have received a copy of the GNU Lesser General Public License

  ~ along with this distribution; if not, write to:

  ~ Free Software Foundation, Inc.

  ~ 51 Franklin Street, Fifth Floor

  ~ Boston, MA  02110-1301  USA

  -->

<ehcache>

 

    <!-- Sets the path to the directory where cache .data files are created.

 

         If the path is a Java System Property it is replaced by

         its value in the running VM.

 

         The following properties are translated:

         user.home - User's home directory

         user.dir - User's current working directory

         java.io.tmpdir - Default temp file path -->

    <diskStore path="./target/tmp"/>

 

 

    <!--Default Cache configuration. These will applied to caches programmatically created through

        the CacheManager.

 

        The following attributes are required for defaultCache:

 

        maxInMemory       - Sets the maximum number of objects that will be created in memory

        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element

                            is never expired.

        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used

                            if the element is not eternal. Idle time is now - last accessed time

        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used

                            if the element is not eternal. TTL is now - creation time

        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache

                            has reached the maxInMemory limit.

 

        -->

    <defaultCache

        maxElementsInMemory="10000"

        eternal="false"

        timeToIdleSeconds="120"

        timeToLiveSeconds="120"

        overflowToDisk="true"

        />

 

    <!--Predefined caches.  Add your cache configuration settings here.

        If you do not have a configuration for your cache a WARNING will be issued when the

        CacheManager starts

 

        The following attributes are required for defaultCache:

 

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.

        maxInMemory       - Sets the maximum number of objects that will be created in memory

        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element

                            is never expired.

        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used

                            if the element is not eternal. Idle time is now - last accessed time

        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used

                            if the element is not eternal. TTL is now - creation time

        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache

                            has reached the maxInMemory limit.

 

        -->

 

    <!-- Sample cache named sampleCache1

        This cache contains a maximum in memory of 10000 elements, and will expire

        an element if it is idle for more than 5 minutes and lives for more than

        10 minutes.

 

        If there are more than 10000 elements it will overflow to the

        disk cache, which in this configuration will go to wherever java.io.tmp is

        defined on your system. On a standard Linux system this will be /tmp"

        -->

    <cache name="sampleCache1"

        maxElementsInMemory="10000"

        eternal="false"

        timeToIdleSeconds="300"

        timeToLiveSeconds="600"

        overflowToDisk="true"

        />

 

    <!-- Sample cache named sampleCache2

        This cache contains 1000 elements. Elements will always be held in memory.

        They are not expired. -->

    <cache name="sampleCache2"

        maxElementsInMemory="1000"

        eternal="true"

        timeToIdleSeconds="0"

        timeToLiveSeconds="0"

        overflowToDisk="false"

        /> -->

 

    <!-- Place configuration for your caches following -->

 

</ehcache>

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.dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">great123</property>

        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.show_sql">true</property>

        <property name=" hibernate.cache.provider_class">

                        org.hibernate.cache.internal.NoCacheProvider

        </property>

        <mapping resource="com/lara/Person.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

app10

package com.lara;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class Util

{

            private static SessionFactory sf;

           

            static

            {

                        Configuration c1 = new Configuration();

                        c1.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");

                       

                       

                        c1.addResource("com/lara/Person.hbm.xml");

                        sf = c1.buildSessionFactory();

            }

            public static Session getSession()

            {

                        return sf.openSession();

            }

           

}

  Saturday(21-01-2017)

hibernate.cfg.xml

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">great123</property>

        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.show_sql">true</property>

        <mapping class="com.lara.Gender"/>

        <mapping class="com.lara.Project"/>

        <mapping class="com.lara.Skill"/>

        <mapping class="com.lara.Education"/>

        <mapping class="com.lara.Address"/>

        <mapping class="com.lara.Employee"/>

    </session-factory>

</hibernate-configuration>

Util.java

public class Util

{

     static SessionFactory sf = null;

     static

     {

          Configuration con = new Configuration().configure();

          ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry();

          sf = con.buildSessionFactory(sr);

     }

 

     public static Session getSession()

     {

          return sf.openSession();

     }

}

package com.lara;

 

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity

@Table(name="GENDER")

public class Gender {

            @Id

            private int id;

            private String name;

           

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getName() {

                        return name;

            }

            public void setName(String name) {

                        this.name = name;

            }

           

}

package com.lara;

 

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity

@Table(name="SKILL")

public class Skill {

            @Id

            private int id;

            private String name;

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getName() {

                        return name;

            }

            public void setName(String name) {

                        this.name = name;

            }

           

           

           

           

           

}

package com.lara;

 

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity

@Table(name="PROJECT")

 

public class Project

{

            @Id

            private int id;

            private String name;

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getName() {

                        return name;

            }

            public void setName(String name) {

                        this.name = name;

            }

           

}

package com.lara;

 

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity

@Table(name="EDUCATION")

public class Education {

            @Id

            private int id;

            private String name;

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getName() {

                        return name;

            }

            public void setName(String name) {

                        this.name = name;

            }

           

           

}

package com.lara;

 

import java.util.Set;

 

import javax.persistence.CascadeType;

import javax.persistence.CollectionTable;

import javax.persistence.Column;

import javax.persistence.ElementCollection;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.JoinTable;

import javax.persistence.ManyToMany;

import javax.persistence.ManyToOne;

import javax.persistence.OneToOne;

import javax.persistence.Table;

 

import org.hibernate.annotations.IndexColumn;

@Entity

@Table(name="EMPLOYEE")

public class Employee

{

            @Id

            @GeneratedValue(strategy=GenerationType.AUTO)

            private int id;

            private String firstName;

            private String lastName;

            private int age;

            @ManyToOne

            @JoinColumn(name="gender_id")

            private Gender gender;

            @ManyToOne

            @JoinColumn(name="education_id")

            private Education education;

            @ManyToOne

            @JoinColumn(name="project_id")

            private Project project;

           

            @ManyToMany(cascade = {CascadeType.ALL})

    @JoinTable(name="B_S",

                joinColumns={@JoinColumn(name="S_ID")},

                inverseJoinColumns={@JoinColumn(name="B_ID")})  

            private Set<Skill> skills;

           

            @OneToOne

            private Address address;

           

            @ElementCollection

            @CollectionTable(name = "mobile_numbers",

                             joinColumns = @JoinColumn(name = "S_ID"))

            @IndexColumn(name="sno")

            @Column(name = "M_NUMBER")

            private long[] mobileNos;

           

            public Gender getGender() {

                        return gender;

            }

            public void setGender(Gender gender) {

                        this.gender = gender;

            }

            public Education getEducation() {

                        return education;

            }

            public void setEducation(Education education) {

                        this.education = education;

            }

            public Project getProject() {

                        return project;

            }

            public void setProject(Project project) {

                        this.project = project;

            }

            public Set<Skill> getSkills() {

                        return skills;

            }

            public void setSkills(Set<Skill> skills) {

                        this.skills = skills;

            }

            public Address getAddress() {

                        return address;

            }

            public void setAddress(Address address) {

                        this.address = address;

            }

            public long[] getMobileNos() {

                        return mobileNos;

            }

            public void setMobileNos(long[] mobileNos) {

                        this.mobileNos = mobileNos;

            }

            public int getId() {

                        return id;

            }

            public void setId(int id) {

                        this.id = id;

            }

            public String getFirstName() {

                        return firstName;

            }

            public void setFirstName(String firstName) {

                        this.firstName = firstName;

            }

            public String getLastName() {

                        return lastName;

            }

            public void setLastName(String lastName) {

                        this.lastName = lastName;

            }

            public void setAge(int age) {

                        this.age = age;

            }

           

            public int getAge() {

                        return age;

            }

           

}

index.jsp

<a href="RegServlet">New Registration</a>

reg.jsp

<%@page import="com.lara.Education"%>

<%@page import="com.lara.Project"%>

<%@page import="com.lara.Skill"%>

<%@page import="java.util.List"%>

<%@page import="com.lara.Gender"%>

<%

     List<Gender> genders = (List<Gender>) request.getAttribute("genders");

     List<Skill> skills = (List<Skill>) request.getAttribute("skills");

     List<Project> projects = (List<Project>) request.getAttribute("projects");

     List<Education> educations = (List<Education>) request.getAttribute("educations");

%>

 

<form action="RegServlet" method="post">

    

     First Name:

     <input type="text" name="firstName"/> </br>

    

     Last Name:

     <input type="text" name="lastName"/> </br>

    

     Age:

     <input type="text" name="age"/> </br>

    

    

     Gender:

          <%

              for(Gender gender : genders)

              {

          %>

                   <input type='radio' name='gender' value='<%= gender.getId() %>'/> <%= gender.getName() %>

          <%

              }

          %> </br>

 

     Skill:

          <%

              for(Skill skill: skills)

               {

          %>

                   <input type='checkbox' name='skills' value='<%= skill.getId() %>'/> <%= skill.getName() %>

          <%

              }

          %> </br>

     Project:

          <%

              for(Project project : projects)

              {

          %>

                   <input type='radio' name='project' value='<%= project.getId() %>'/> <%= project.getName() %>

          <%

              }

          %> </br>

     Education:

          <%

              for(Education education : educations)

              {

          %>

                   <input type='radio' name='education' value='<%= education.getId() %>'/> <%= education.getName() %>

          <%

              }

          %> </br>

 

     Mobile Numbers:

     <input type='text' name='mobileNos'/> </br>

     <input type='text' name='mobileNos'/> </br>

    

     House No:

     <input type='text' name='houseNo'/> </br>

    

     Street Name:

     <input type='text' name='streetName'/> </br>

 

     City:

     <input type='text' name='city'/> </br>

 

     <input type='submit' value='submit'/>

</form>

package com.lara;

 

import java.io.IOException;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.hibernate.Session;

 

/**

 * Servlet implementation class RegServlet

 */

public class RegServlet extends HttpServlet {

            private static final long serialVersionUID = 1L;

      

    /**

     * @see HttpServlet#HttpServlet()

     */

    public RegServlet() {

        super();

        // TODO Auto-generated constructor stub

    }

 

            /**

             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

             */

            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                        Session session = Util.getSession();

                        List<Gender> genders = session.createCriteria(Gender.class).list();

                        List<Skill> skills = session.createCriteria(Skill.class).list();

                        List<Education> educations = session.createCriteria(Education.class).list();

                        List<Project> projects = session.createCriteria(Project.class).list();

                       

                        request.setAttribute("genders", genders);

                        request.setAttribute("skills", skills);

                        request.setAttribute("educations", educations);

                        request.setAttribute("projects", projects);

                        request.getRequestDispatcher("reg.jsp").forward(request, response);

            }

 

            /**

             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

             */

            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                        String firstName = request.getParameter("firstName");

                        String lastName = request.getParameter("lastName");

                        String age = request.getParameter("age");

                        String gender = request.getParameter("gender");

                        String[] skills = request.getParameterValues("skills");

                        String project = request.getParameter("project");

                        String education = request.getParameter("education");

                        String[] mobileNos = request.getParameterValues("mobileNos");

                        String houseNo = request.getParameter("houseNo");

                        String streetName = request.getParameter("streetName");

                        String city = request.getParameter("city");

                        Address add = new Address();

                        add.setHouseNo(houseNo);

                        add.setStreetName(streetName);

                        add.setCity(city);

                        Employee emp = new Employee();

                        emp.setAddress(add);

                        add.setEmployee(emp);

                        emp.setFirstName(firstName);

                        emp.setLastName(lastName);

                        emp.setAge(Integer.parseInt(age));

                        Gender g1 = new Gender();

                        g1.setId(Integer.parseInt(gender));

                        emp.setGender(g1);

                        Education e1 = new Education();

                        e1.setId(Integer.parseInt(education));

                        emp.setEducation(e1);

                       

                        Project pr = new Project();

                        pr.setId(Integer.parseInt(project));

                        emp.setProject(pr);

                       

                        Set<Skill> skillsObjs = new HashSet<>();

                        Skill skill;

                        for(int i = 0; i < skills.length; i++)

                        {

                                    skill = new Skill();

                                    skill.setId(Integer.parseInt(skills[i]));

                                    skillsObjs.add(skill);

                        }

                        emp.setSkills(skillsObjs);

                        long[] mobileNosLong = new long[mobileNos.length];

                        for(int i = 0; i < mobileNos.length; i++)

                        {

                                    mobileNosLong[i] = Long.parseLong(mobileNos[i]);

                        }

                        emp.setMobileNos(mobileNosLong);

                        Session session = Util.getSession();

                        System.out.println("start");

                        session.beginTransaction();

                        session.save(emp);

                        session.save(add);

                        session.getTransaction().commit();

                        System.out.println("end");

            }

 

}