Followers

hb : Many to One Xml Mapping

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

package myhb;

public class Address {
    private int addrId;
    private String city;

    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;
    }
}


Employee.java
==============

package myhb;

public class Employee {
    private int empId;
    private String name;
    private Address address;

    public int getEmpId() {
        return empId;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

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


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="myhb.Address" table="Address101x">
    <id column="addrId" name="addrId"/>
    <property column="city" length="20" name="city"/>
    
  </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="myhb.Employee" table="Employee101x">
    <id column="empId" name="empId"/>
    <property column="name" length="20" name="name"/>
    
    <many-to-one name="address" class="myhb.Address" column="foreignkey" cascade="all" ></many-to-one>
  </class>
</hibernate-mapping>


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/manytoone</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">create</property>
    <mapping resource="employee.hbm.xml"/>
    <mapping resource="address.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


OurLogic.java
=====================

package myhb;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class OurLogic {

public static void main(String args[])
{

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml"); 
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
                Address addr=new Address();
addr.setAddrId(500);
addr.setCity("Bangalore");
Employee e1=new Employee();
e1.setEmpId(100);
e1.setName("James");
                e1.setAddress(addr);
Employee e2=new Employee();
e2.setEmpId(101);
e2.setName("Lee");
                e2.setAddress(addr);
                Transaction tx = session.beginTransaction();
session.save(e1);
session.save(e2);
tx.commit();
session.close();
System.out.println("One To Many ..!!");
factory.close();            
}
}

No comments:

Post a Comment