Followers

hb : OneToOne mapping using Annotation (Unidirectional)

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


Address.java
============
package com.myhb;

import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "ADDR999")
public class Address implements Serializable{
    @Id
    @Column(name = "ADDRID")
    private int addrId;
    @Column(name = "LINE1")
    private String line1;
    @Column(name = "CITY")
    private String city;

    @Column(name = "PINCODE")
    private int pincode;
    public Address(){
    
    }
    public int getAddrId() {
        return addrId;
    }

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

    public String getLine1() {
        return line1;
    }

    public void setLine1(String line1) {
        this.line1 = line1;
    }

    public String getCity() {
        return city;
    }

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

    public int getPincode() {
        return pincode;
    }

    public void setPincode(int pincode) {
        this.pincode = pincode;
    }
    public String toString(){
    return addrId+" "+line1+" "+city+" "+pincode;
    }
    
}

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

package com.myhb;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.*;
@Entity
@Table(name = "EMP999")
public class Employee implements Serializable{
    @Id
    @Column(name = "EMPID")
    private int empId;
    @Column(name = "EMPNAME")
    private String name;
    @Column(name = "SALARY")
    private float salary;
    @OneToOne(targetEntity = Address.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "ADDRID_FK", referencedColumnName = "ADDRID")
    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 getName() {
        return name;
    }

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

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 83 * hash + this.empId;
        hash = 83 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Employee other = (Employee) obj;
        if (this.empId != other.empId) {
            return false;
        }
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }
}

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

package com.myhb;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class SaveData {
    public static void main(String[] args) {
        SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        Employee emp = new Employee();
        Address addr = new Address();
        emp.setEmpId(333);
        emp.setName("Mohan");
        emp.setSalary(20000);
        addr.setAddrId(888);
        addr.setCity("Bengaluru");
        addr.setLine1("MunneKolala");
        addr.setPincode(900009);
        emp.setAddress(addr);
        session.persist(addr);
        session.persist(emp);
        tx.commit();
        session.close();
        factory.close();
        System.out.println("Record saved successfully...");
        
    }
    
}

DeleteOrUpdate.java
===================

package com.myhb;

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

public class DeleteOrUpdate {
    public static void main(String[] args) {
        SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        
        Employee emp = (Employee)session.load(Employee.class, 333);
        session.delete(emp);
      //  Address addr = (Address)session.load(Address.class, 888);
        //session.delete(addr);
        tx.commit();
        session.close();
        System.err.println("Record deleted successfully...");
    }
    
}

FetchData.java
==============

package com.myhb;

import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class FetchData {
    public static void main(String[] args) {
        SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery("From Employee");
        List list = q.list();
        Iterator itr = list.iterator();
        while(itr.hasNext()){
            Employee emp = (Employee) itr.next();
        System.out.println(emp.getEmpId()+" "+emp.getName()+" "+emp.getSalary());
            System.out.println("Address : "+emp.getAddress());           
            
        }
        System.err.println("Records retrieval over...");
        
    }
    
}

No comments:

Post a Comment