In persistent units we often face a scenario where we think of grouping few similar properties and creating separate persistent entity for them would be expensive. For such situation creating embedded components for grouping such properties makes more sense. The life cycle of such components are managed by the owning components. Embedded components don’t have their own object identity and they are totally dependent on the owning persistent entities. For example let’s take a scenario. We have a Person entity which should create home and billing address. Address will comprise of street, city and zipcode. Either we can add six properties in Person or we can create embedded components for home and billing address. Let’s take a look at the Person POJO.

public class Person {

    private Long id;
    private String firstName;
    private String lastName;
    private Address homeAddress;
    private Address billingAddress;

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Address getHomeAddress() {
        return homeAddress;
    }

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

    public Address getBillingAddress() {
        return billingAddress;
    }

    public void setBillingAddress(Address billingAddress) {
        this.billingAddress = billingAddress;
    }
}

Here’s the Address POJO.

public class Address {

    private String street;
    private String city;
    private String zipcode;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

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

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public String toString(){
        return ""+this.getStreet()+" : "+this.getCity()+" : "+this.getZipcode();
    }
}

Finally, let’s take a look at the mapping XML person.hbm.xml for Person:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="hello.Person"
           table="PERSONS">

        <id name="id" type="long" column="PERSON_ID">
            <generator class="native"/>
        </id>

        <property name="firstName" type="java.lang.String" column="PERSON_FIRSTNAME"/>
        <property name="lastName" type="java.lang.String" column="PERSON_LASTNAME"/>

        <component name="homeAddress" class="hello.Address">
            <property name="street" type="string" column="PERSON_HOME_CITY"/>
            <property name="city" type="string" column="PERSON_HOME_STREET"/>
            <property name="zipcode" type="string" column="PERSON_HOME_ZIPCODE"/>
        </component>

        <component name="billingAddress" class="hello.Address">
            <property name="street" type="string" column="PERSON_BILLING_CITY"/>
            <property name="city" type="string" column="PERSON_BILLING_STREET"/>
            <property name="zipcode" type="string" column="PERSON_BILLING_ZIPCODE"/>
        </component>
    </class>
</hibernate-mapping>

To add rest of the configuration you can take a look at one of my previous posts on Hibernate here.
Let’s put all the code into action with a main class.

public class App {
        private static final Logger log = Logger.getLogger(App.class);
    public static void main(String... args){
        /* first unit of db work */
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        Person person = new Person();
        person.setFirstName("Prashant");
        person.setLastName("Gupta");

        Address homeAddress = new Address();
        homeAddress.setCity("Delhi");
        homeAddress.setStreet("XYZ");
        homeAddress.setZipcode("1100XY");

        Address billingAddress = new Address();
        billingAddress.setCity("Noida");
        billingAddress.setStreet("ABC");
        billingAddress.setZipcode("PQRS");

        person.setHomeAddress(homeAddress);
        person.setBillingAddress(billingAddress);
        Long id = (Long) session.save(person);
        tx.commit();
        session.close();

        log.info("======================================================= " + id);
        Message message = new Message();
        session =  HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        Person person1 = (Person)session.createQuery("from Person p where p.id = ? ")
                .setLong(0,id)
                .uniqueResult();
        log.info("================ person first============ "+person1.getFirstName());
        log.info("================ person last ============ "+person1.getLastName());
        log.info("================ person home address ============ "+person1.getHomeAddress());
        log.info("================ person billing address ============ "+person1.getBillingAddress());
        session.save(message);
        tx.commit();
        session.close();

    }
}