Followers

hb : OneToOne XMl mapping (Bidirectional)

Address.java
============

package mypack;

public class Address {
    private int addrID;
    private String city;
    private String state;
    private Employee employee;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    
    public int getAddrID() {
        return addrID;
    }

    public void setAddrID(int addrID) {
        this.addrID = addrID;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
    
}

Employee.java
=============
package mypack;

public class Employee {
    private int empId;
    private String empName;
    private Address address;
    
    public Employee(){}

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }
    
    
}

hibernate.cfg.xml
=================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/hibernate</property>
    <property name="hibernate.connection.username">app</property>
    <property name="hibernate.connection.password">app</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="address.hbm.xml" />
    <mapping resource="employee.hbm.xml"/>
  </session-factory>
</hibernate-configuration>



address.hbm.xml
================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="mypack.Address" table="ADDRXMLB" >
        <id name="addrID" >
            <generator class="increment" />            
        </id>
        <property name="city" />
        <property name="state" />
        <one-to-one name="employee" cascade="all" />
    </class>
</hibernate-mapping>


employee.hbm.xml
================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="mypack.Employee"  table="EMPXMLB" >
        <id name="empId" >
            <generator class="increment" ></generator>            
        </id>
        <property name="empName" />     
    <one-to-one name="address" class="mypack.Address" cascade="all" />
    </class>
</hibernate-mapping>


InsertingRecords.java
=====================

package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class InsertingRecords {
    public static void main(String[] args){
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    Employee emp = new Employee();
    Address addr = new Address();
    emp.setAddress(addr);
    emp.setEmpName("HARI999");
    addr.setCity("Bengaluruhdsa3232");
    addr.setState("Karna111");
   // session.persist(addr);
    session.persist(emp);
    tx.commit();
    session.close();
    System.out.println("Record saved successfully...");
    
    }
    
}


UpdateOrDelete.java
===================

package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class UpdateOrDelete {
    public static void main(String[] args){
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    Employee emp = (Employee)session.load(Employee.class,1);
    session.delete(emp);
    
   // Address addr = (Address)session.load(Address.class,2);
   // session.delete(addr);
    
   // session.persist(emp);
    tx.commit();
    session.close();
    System.out.println("Record deleted successfully...");
    
    }
    
}



No comments:

Post a Comment