Followers

hb : One to Many Using Annotation

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

package mypackage;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "ADDRESSA")
public class Address {
    @Id
    @Column(name = "ADDR_ID")
    private int addrId;
    @Column(name = "ADDR_NAME")
    private String addrName;

    public int getAddrId() {
        return addrId;
    }

    public void setAddrId(int addrId) {
        this.addrId = addrId;
    }

    public String getAddrName() {
        return addrName;
    }

    public void setAddrName(String addrName) {
        this.addrName = addrName;
    }
}



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

package mypackage;

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "EMPLOYEEA")
public class Employee {
    @Id
    @Column(name = "EMP_ID")
    private int empId;
    @Column(name = "EMP_NAME")
    private String empName;
    @OneToMany(targetEntity = Address.class,cascade = CascadeType.ALL)
    @JoinColumn(name = "EMP_ID_FK" , referencedColumnName = "EMP_ID")
    private Set 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;
    }

    public Set getAddress() {
        return address;
    }

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



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/ONETOMANY</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 class="mypackage.Address" />
    <mapping class="mypackage.Employee" />
  </session-factory>
</hibernate-configuration>



SaveData.java
=============

package mypackage;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.cfg.*;
import org.hibernate.*;

public class SaveData {
       
    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 addr1 = new Address();
    Address addr2 = new Address();
    addr1.setAddrId(11111);
    addr1.setAddrName("Mohan1111");
    addr2.setAddrId(33333);
    addr2.setAddrName("Mohan3333");
    emp.setEmpId(22222);
    emp.setEmpName("Bengaluru");
    Set set = new HashSet();
    set.add(addr1);
    set.add(addr2);
    emp.setAddress(set);
    session.save(emp);
    tx.commit();
    session.close();        
    factory.close();
    System.out.println("Saved the data successfully...");
    }    
}


No comments:

Post a Comment