Followers

hb : Many to One to Annotation

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

package mypack;

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

@Entity
@Table(name = "ADDR199")
public class Address {
@Id
@Column(name = "addrId")
private int addrId;
@Column(name = "addrName")
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 mypack;

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

@Entity
@Table(name = "EMP199")
public class Employee {
    @Id
    @Column(name = "empId")
    private int empId;
    @Column(name = "empName")
    private String empName;
    @ManyToOne(targetEntity = Address.class,cascade = CascadeType.ALL)
    @JoinColumn(name ="addrId_FK", referencedColumnName = "addrId")
    private 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;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address 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/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 class="mypack.Address" />
    <mapping class="mypack.Employee" />
  </session-factory>
</hibernate-configuration>


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

package mypack;
import org.hibernate.cfg.*;
import org.hibernate.*;
public class SaveData {
    SessionFactory factory;
public static void main(String[] args){
    SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
SaveData MTO = new SaveData();
MTO.saveData(factory);
factory.close();

}
public void saveData(SessionFactory factory){
    this.factory = factory;
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    Employee emp1 = new Employee();
    Employee emp2 = new Employee();
    Address addr = new Address();
    emp1.setEmpId(1111);
    emp1.setEmpName("Mohan1111");
    emp1.setAddress(addr);
    emp2.setEmpId(2222);
    emp2.setEmpName("Mohan2222");
    addr.setAddrName("AddressName");
    addr.setAddrId(9999);
    emp2.setAddress(addr);
    session.save(emp1);
    session.save(emp2);    
    tx.commit();
    session.close();
    System.out.println("Saved data successfully...");
}
}

No comments:

Post a Comment