Embedded Objects and Value Types

Embedding Objects using Hibernate

In the previous tutorials, we have learned various things about Hibernate, a list of them is shown below.

  • Brief introduction
  • Setting up hibernate
  • Getting started with Hibernate API – JPA
  • Elaboration of Hibernate configuration file
  • Explanation of Hibernate Annotations
  • Create/Read Operation on a database with Hibernate
  • Use of primary keys with hibernate.

The example that we have taken allows us to store class level variable as table columns in the database. These class level variables are simple with data types like strings , date etc. We know that in classes we can have other objects as class level variables we can also have arrays and collections as a class level variables, now the question arises. How will we store these complex variables in the database with hibernate?  In this tutorial and some other upcoming tutorials, we will try to find the answer to this question.

Example Embedding Objects 

Let’s try to understand with the example, We will create a new class named “Address.java” it will have four class level variables “Street, City , Distinct, Country”. In the previous example, we have the user class with name and Id as class level variables. In this example, we will add this address class object in our User Class as a class level variable with name and ID and try to save it.

When dealing with hibernate we see two types of objects Value Objects and Entity. Before we move further we have to know the difference between them.

Entity

It is a class that needs to be saved as a separate table in the database. An Independent class that has its own meaning is called as an Entity. Data present inside the entity provides the meaning of itself.

Value Objects

It is an Object that has data and needs to be saved in the database but doesn’t have to mean as of itself. It is a supportive object that adds meaning to another object.

@Embeddable and @Embedded

In our Example we have an Address object, this object independently doesn’t have a meaning. We have to use its object with another entity to complete its meaning. The purpose of this object is to provide value to the user class. Steps for creating and editing the address class is as follows

  • Create a new java class named Address.
  • Define for class level variables/fields in the created class all with data type string and access modifiers set as private.
  • Generate gets and setters for these fields.
  • We will use annotation “@Embeddable” with this Address class to tell hibernate that its object needs to be embedded in somewhere else.
  • Hibernate provides this functionality in javax.persistance.Embeddable

Address.java

Now In the User Class, we will perform following updates.

  • We will create an object of this Address class
  • Set its access modifier to private.
  • Generate getters and setters for the address object. Here we can also use “@Embedded” annotation with this address object to tell hibernate that t needs to be embedded. If we have used @ Embeddable in the address Class then this annotation is not mandatory.

UserDetails.java

Finally, in our Main class, we will add following things.

  • Create an address object
  • Set the value of Street, City , Distinct, Country by Using this object.
  • Add this object to the user’s object that we previously have.
  • Ultimately Save the user object using Session Object.
  • Now we are ready to run the example.

  HibernateMain.java

hibernate.cfg.xml

This file will remain the same as our previous examples.

Test Run:

Right, click on HibernateMain.java click RunAs and then RunAsServer.

Console

Now open the pgAdmin III to view the inserted records.

view-inserted-data-using-pgadminiii-tool

We can see the record is inserted successfully.

inserted-data

Summary:

In this tutorial, we have accomplished following things.

  • Learned about Value Objects and Entity.
  • How to Embed an object within another using Hibernate.
  • A Complete Example of @Embeddable and @Embedded Annotations .

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

About Author: Uzair Ahmed Khan is Technical Content Writer, Computer Software Blogger, and Software Engineer. He has a keen eye on Java Technologies and other Related Developments.

Leave a Reply

Your email address will not be published. Required fields are marked *