[Example-Lab] Embedding Value Types Within an Entity Class in Hibernate 4.3

A value type is a type which doesn't have existence of its own, but belongs to an entity, like an user and his address. Value types are always completely owned by their containing entity.

We will embed an Address class inside a User class. The simplest way to embed a value type in an entity class is to treat the member variables of the embedded class (e.g. Address), the same way as we treat the member variables of the embedding class (e.g. User) by adding more columns to the entity table one column each for the value type class members

Annotate the embedded Address class as @Embeddable and annotate the reference variable to Address class as @Embedded in the embedding class.

Hibernate will treat the member variables of the embedded Address class, the same way as we treat the member variables of the embedding User class, and create additional columns in User table for each of the members of Address class.

If you are not following the tutorials in order, refer to the article http://javajee.com/your-first-hibernate-program for the basic setup including hibernate configuration file. We have used MySQL database and if you are using a different one, change the configuration file accordingly.

 

Address Class

We will create one more class called Address as below:

package com.javajee.hiberex.dto;

 

import javax.persistence.Embeddable;

 

@Embeddable

public class Address {

 

  private int houseno;

  private String street;

 

  public int getHouseno() {

    return houseno;

  }

  public void setHouseno(int houseno) {

    this.houseno = houseno;

  }

  public String getStreet() {

    return street;

  }

  public void setStreet(String street) {

    this.street = street;

  }

}

 

User Class

We will now modify the class User.java as below:

package com.javajee.hiberex.dto;

 

import javax.persistence.Embedded;

import javax.persistence.Entity;

import javax.persistence.Id;

 

@Entity

public class User {

 

  @Id

  int id;

  private String name;

  @Embedded

  private 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;

  }

  public Address getAddress() {

    return address;

  }

  public void setAddress(Address address) {

    this.address = address;

  }

}

 

Test Class

We will also modify the test class as below:

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.javajee.hiberex.dto.Address;

import com.javajee.hiberex.dto.User;

 

public class UserTest {

 

  public static void main(String[] args) {

 

    User u = new User();

    u.setId(1);

    u.setName("Heartin");

    Address addr = new Address();

    addr.setHouseno(777);

    addr.setStreet("MyStreet");

    u.setAddress(addr);

    Configuration configuration = new Configuration();

    configuration.configure();

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    Session session = sessionFactory.openSession();

    session.beginTransaction();

    session.save(u);

    session.getTransaction().commit();

    u = null;

    session = sessionFactory.openSession();

    session.beginTransaction();

    u = (User) session.get(User.class, 1);

    session.getTransaction().commit();

    System.out.println("Details retrieved:" + u.getName() + " "

        + u.getAddress().getHouseno() + " " + u.getAddress().getStreet());

  }

}

 

Run the program and verify the results.

You can also verify the corresponding tables in the example.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!