Tuesday, June 14, 2016

Embeddable property in Hibernate

package com.javadsalgo.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="USERDETAILS")
public class UserDetails {

      @Id
      private int id;
      private String name;
      private Address address;

      public Address getAddress() {
            return address;
      }

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

      public int getId() {
            return id;
      }

      public void setId(int id) {
            this.id = id;
      }

      public String getName() {
            return name;
      }

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

}






package com.javadsalgo.model;

import javax.persistence.Embeddable;
import javax.persistence.Entity;

@Embeddable
public class Address {
     
      private String streetName;
      private String building;

      public String getStreetName() {
            return streetName;
      }

      public void setStreetName(String streetName) {
            this.streetName = streetName;
      }

      public String getBuilding() {
            return building;
      }

      public void setBuilding(String building) {
            this.building = building;
      }

}

package com.javadsalgo.Client;

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

import com.javadsalgo.model.Address;
import com.javadsalgo.model.UserDetails;

public class HibernateTestEmbeddable {

      public static void main(String[] args) {
            UserDetails user = new UserDetails();
            user.setName("Paras");

            Address add = new Address();
            add.setBuilding("Twin Tower");
            add.setStreetName("Strret no 5");
            user.setAddress(add);

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            session.close();

      }
}



package com.javadsalgo.model;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name = "USERDETAILS1")
public class UserDetails1 {

      @Id
      private int id;
      private String name;

      @Embedded
      @AttributeOverrides({
                  @AttributeOverride(name = "streetName", column = @Column(name = "HOME_STREET_NAME")),
                  @AttributeOverride(name = "building", column = @Column(name = "HOME_BUILIDING")) })
      private Address homeAddress;

      @Embedded
      private Address officeAddress;

      public Address getHomeAddress() {
            return homeAddress;
      }

      public void setHomeAddress(Address homeAddress) {
            this.homeAddress = homeAddress;
      }

      public Address getOfficeAddress() {
            return officeAddress;
      }

      public void setOfficeAddress(Address officeAddress) {
            this.officeAddress = officeAddress;
      }

      public int getId() {
            return id;
      }

      public void setId(int id) {
            this.id = id;
      }

      public String getName() {
            return name;
      }

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

}

package com.javadsalgo.Client;

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

import com.javadsalgo.model.Address;
import com.javadsalgo.model.UserDetails;
import com.javadsalgo.model.UserDetails1;

public class HibernateTestEmbeddable2 {

      public static void main(String[] args) {
            UserDetails1 user = new UserDetails1();
            user.setName("Paras");

            Address addr = new Address();
            addr.setBuilding("Twin Tower");
            addr.setStreetName("Strret no 5");

            Address addr2 = new Address();
            addr2.setBuilding("New Twin Tower");
            addr2.setStreetName("New Strret no 5");

            user.setHomeAddress(addr);
            user.setOfficeAddress(addr2);
            SessionFactory sessionFactory = new   Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            session.close();

      }
}

package com.javadsalgo.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
     
      @Column(name="STREET_NAME")
      private String streetName;
      @Column(name="STREET_BUILIDING")
      private String building;

      public String getStreetName() {
            return streetName;
      }

      public void setStreetName(String streetName) {
            this.streetName = streetName;
      }

      public String getBuilding() {
            return building;
      }

      public void setBuilding(String building) {
            this.building = building;
      }

}


No comments:

Post a Comment