Monday 30 November 2015

@Embeddable Classes in Action

Share & Comment





Table of Content :


Introduction :

JPA allows the persistence of fields as embedded typically into the same table as the "owning" class using @Embedded objects, which allow you to pull out attributes of an entity into another class, and embed that class back into the entity. The result is a Java object that might look a little more logical, but doesn’t actually change the database structure since the embedded object’s fields are still on the first object.

To illustrate this, imagine we have a Person object who has an Address.

Person Entity with address fields

It might make sense to pull out the address into another class, but maybe for speed or legacy reasons we have to keep a denormalized table structure. Using an Embeddable class (with the @Embeddable annotation), we can still do this on the JPA end of things. It would look like this:

Person Entity and Address Embeddable
Instances of embeddable classes are always embedded in other entity objects and do not require separate space allocation and separate store and retrieval operations. Therefore, using embeddable classes can save space in the database and improve efficiency.

Create the JPA Diagram :

  1. Goto File menu > New File > Persistence category .
    Create JPA Diagram
  2. From the Persistence , select JPA Diagram and click Next. 
  3. Type SmapleERD for the diagram name. 
  4. Type com.jpamodeler.embeddable for the Package. 
  5. Click Finish. 
When we click Finish, the NetBeans IDE creates the JPA Diagram and opens the diagram in the designer window.

Create the Embedded Property:

The @Embedded annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class. A class is declared as embeddable by marking it with the @Embeddable annotation. 
  1. Drag & Drop the Entity and Embeddable from the Palette window to designer. 
    Palette
  2. Double click on the Entity and Embeddable and rename to Person and Address . 
  3. To create basic attribute, click on the Person Entity  , then click on the expand   button > basic  property.
  4. To create embedded attribute, click on the Person Entity , then click on the composition button.
    Add embedded property
  5. Now, drag embedded attribute to Address Embeddable class.
    Embedded property
  6. Right click on the diagram > Generate Source Code.
    Generate Source Code
  7. Click on the Person entity and Address embeddable class to view generated source code.
  8. Now, deploy the project and view the database table.
    Database
JPA will just treat all of the attributes of Address as attributes of Person, and it all works out. JPQL queries should change, but for the most part all of your Java code looks a lot more logical. None of your queries will have any joins.


Nested Embeddable Classes :

A nested embeddable is a relationship to an embeddable object from another embeddable.
  1. Drag & Drop the Embeddable from the Palette window to designer.
  2. To create nested embedded property.Click on the Address Embeddable , then click on the composition button.
    Add nested embedded property
  3. Now, drag embedded attribute to ZipCode Embeddable class. 
    Nested embedded property
  4. Right click on the diagram > Generate Source Code.
    Generate Source Code
  5. Click on the Address and ZipCode embeddable class to view generated source code.
  6. Now, deploy the project and view the database table.
    Database


Shared Embeddable :

An embeddable object can be shared between multiple classes or within class. By default, column definitions specified in the @Embeddable class apply to the table of the owning entity but you can override them using @AttributeOverride.

Consider Person has property home address and office address of type Address. If Entity have the same embeddable object type twice, then default column name will not work, at least one of the columns will have to be explicit. To support this, @AttributeOverride can be applied to an embedded field or property to override mapping defined by the embeddable class (or embeddable class of one of its attributes).

  1. First rename the address property to homeAddress by double click on it.
  2. Drag embedded attribute to Address Embeddable class , to add one more embedded property named officeAddress.
    homeAddress and officeAddress property
  3. Right click on the diagram > Generate Source Code.
    Generate Source Code
  4. Click on the Person entity class to view generated source code.
  5. Now, deploy the project and see the server log .
The person table has two attributes (homeAddress.street and officeAddress.street) that both have the same column name (“street”). We have to define different column names for homeAddress and officeAddress attributes. To do that, use @AttributeOverride (and if our embedded object has a relationship to another entity, use @AssociationOverride).

Add AttributeOverride Annotation :
  1. Open the diagram and click on the Person Entity 
  2. Now, open the JPA Override View panel from Menu > Window > JPA Modeler > Override View.
    JPA Override View Panel
  3. In this panel, you may view all of the attribute on which Attribute Override and Association Override annotation can be applied.
    Change column name of homeAddress.street
  4. Click on the attribute and change their column name from property panel.
    Change column name of officeAddress.street
  5. Generate the source code and view Person entity class . 
  6. Now, deploy the project and view the database table .
    Database

Restriction :

Embeddable classes, however, do not have an identity (primary key) of their own which leads to some limitations :
  • Embeddable instances cannot be shared by different entity objects.
  • Embeddable objects cannot be directly persisted, it can only be persisted or queried in the context of their parent.
  • Embeddable objects does not have an id or table.
So a decision whether to declare a class as an entity or embeddable requires case by case consideration.

Download Source Code :

Github - Embeddable Classes



Tags:

Author -

Shiwani is a software engineer with a passion for learning. She is an enthusiast of netbeans and new technologies and developer of JPA Modeler.

0 comments:

 
Copyright © JPA Modeler | Designed by Templateism.com