liveBook · Manning

Embedded component

As explained in Java Persistence with Spring Data and Hibernate epub

An embedded entity is a value-like persistent object whose fields are stored in the same database table as its owning entity rather than in a separate table.

An embedded component is a value-like persistent object whose fields are stored in the owning entity’s table rather than a separate table. It has no independent identity, cannot be shared, and follows the owner’s lifecycle. section 6.1 section 6.2.1

A User with an embedded Address therefore uses one USERS row containing the user columns and address columns. Saving or deleting the User persists or deletes the address data without separate SQL. section 6.2.1

Configure embedded mappings according to the persistence API:

  • With JPA or Hibernate, mark the value class @Embeddable or map the property with @Embedded. section 6.1
  • When one entity embeds the same value type more than once, use @AttributeOverride to assign distinct column names. section 6.2.3
  • With Spring Data JDBC, omit @Table from the embedded class and configure the property with @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL). section 15.3.2

Two addresses in one entity

A User can embed both homeAddress and billingAddress. The second Address requires columns such as BILLING_STREET, BILLING_ZIPCODE, and BILLING_CITY; the override replaces the Address class’s persistence annotations for those properties. section 6.2.3

Listing 6.9 Overriding the column names
Path: Ch06/mapping-value-types/src/main/java/com/manning/javapersistence
 /ch06/model/User.java
 
@Entity
@Table(name = "USERS")
public class User {
    @Embedded                                                       #1
    @AttributeOverride(name = "street",                             #2
        column = @Column(name = "BILLING_STREET"))                  #2
    @AttributeOverride(name = "zipcode",                            #2
        column = @Column(name = "BILLING_ZIPCODE", length = 5))     #2
    @AttributeOverride(name = "city",                               #2
        column = @Column(name = "BILLING_CITY"))                    #2
    private Address billingAddress;
 
    public Address getBillingAddress() {
        return billingAddress;
    }
 
    public void setBillingAddress(Address billingAddress) {
        this.billingAddress = billingAddress;
    }
    //  . . .
}
#1 The <kbd>billingAddress</kbd> field is marked as embedded. The <kbd>@Embedded</kbd> annotation actually isn’t necessary. You can mark either the component class or the property in the owning entity class (using both doesn’t hurt but it offers no advantage). The <kbd>@Embedded</kbd> annotation is useful if you want to map a third-party component class without a source and no annotations but using the right getter and setter methods (like regular JavaBeans).
#2 The repeatable <kbd>@AttributeOverride</kbd> annotation selectively overrides property mappings of the embedded class. In this example we override all three properties and provide different column names. Now we can store two <kbd>Address</kbd> instances in the <kbd>USERS</kbd> table, with each instance in a different set of columns (look back at the schema again in figure 6.2).

Inspect the Java mapping that overrides the billing address columns. section 6.2.3

Listing 15.20 The schema.sql file for embedded entities
Path: Ch15/spring-data-jdbc4/src/main/resources/schema.sql
 
DROP TABLE IF EXISTS USERS;
 
CREATE TABLE USERS (
   ID INTEGER AUTO_INCREMENT PRIMARY KEY,
   ACTIVE BOOLEAN,
   USERNAME VARCHAR(30),
   EMAIL VARCHAR(30),
   LEVEL INTEGER,
   REGISTRATION_DATE DATE,
   STREET VARCHAR(30) NOT NULL,
    CITY VARCHAR(20) NOT NULL
);

Inspect the Spring Data JDBC schema that stores address fields directly in USERS. section 15.3.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains table layout, lifecycle, column overrides, null handling, repositories, and tests across JPA and Spring Data JDBC.
Examples
Uses User, Address, USERS, homeAddress, billingAddress, overridden BILLING_* columns, repositories, schemas, and persistence tests.
What you can do after
Operate/configure embedded mappings and persist them through JPA, Hibernate, Spring Data JPA, or Spring Data JDBC repositories.
How it connects
Connects value types, entities, composition relationships, attribute overrides, Spring Data JPA, Spring Data JDBC, and repositories.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.