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) { = 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) { = 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"
    <class name="hello.Person"

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

        <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 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"/>

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();

        Address homeAddress = new Address();

        Address billingAddress = new Address();

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

        Person person1 = (Person)session.createQuery("from Person p where = ? ")
                .uniqueResult();"================ person first============ "+person1.getFirstName());"================ person last ============ "+person1.getLastName());"================ person home address ============ "+person1.getHomeAddress());"================ person billing address ============ "+person1.getBillingAddress());;