Hibernate.orgCommunity Documentation

Chapter 4. Compositions

Table of Contents

4.1. Component / Embedded
4.2. Multiple compositions
4.2.1. JPA's AttributeOverride
4.2.2. ImplicitNamingStrategy
4.3. Collections of compositions
4.4. Compositions as Map key
4.5. Compositions as identifiers

Historically Hibernate called these components. JPA calls them embeddables. Either way the concept is the same: a composition of values. For example we might have a Name class that is a composition of first-name and last-name, or an Address class that is a composition of street, city, postal code, etc.

Example 4.1. Simple composition example

@Embeddable
public class Name {
	private String firstName;
	private String middleName;
	private String lastName;
	...
}
@Embeddable
public class Address {
	private String line1;
	private String line2;
	@Embedded
	private ZipCode zipCode;
	...

	@Embeddable
	public static class Zip {
		private String postalCode;
		private String plus4;
		...
	}
}

A composition is another form of value type. The lifecycle of a composition is defined by the thing that contains it.

A composition inherits the attribute access of its parent. For details on attribute access, see ???.

Compositions can be made up of basic values as well as associations, with the caveat that compositions which are used as collection elements cannot themselves define collections.

This is the form of composition you will see most often. Here an entity or another composition is the container.


Note

Notice that JPA defines 2 terms for composition: Embeddable and Embedded. Embeddable is used to describe the composition class itself (Name). Embedded is used to describe a usage of that composition (Person.name).

The composition here is the Name type related to Person.name.


The composed values are mapped to the same table as the parent table. Composition is part of good OO data modeling (idiomatic java). In fact that table could also be mapped by the following entity instead.


The composition form is certainly more OO. And that becomes more evident as we work with multiple compositions.


It is certainly more convenient to work with the compositions. However, an interesting thing happens in this particular example. By default, this mapping actually will not work as-is. The problem is in how JPA defines implicit naming rules for columns that are part of a composition, which say that all of the Address compositions would map to the same implicit column names.

This occurs any time we have multiple compositions based on the same embeddable in a given parent. We have a few options to handle this issue.

The JPA-defined way to handle this situation is through the use of its AttributeOverride annotation.


Now, essentially there are no implicit column names in the Address compositions. We have explicitly named them.

Hibernate naming strategies are covered in detail in ???. However, for the purposes of this discussion, Hibernate has the capability to interpret implicit column names in a way that is safe for use with multiple compositions.


Now the "path" to attributes are used in the implicit column naming.


You could even develop your own to do special implicit naming.

Collections of compositions are specifically value collections (as compositions are a value type). Value collections are covered in detail in Section 5.2, “Collections of value types”.

The one thing to add to the discussion of value collections in regards to compositions is that the composition cannot, in turn, define collections.

Compositions can also be used as the key values for Maps. Mapping Maps and their keys is convered in detail in ???.

Again, compositions used as a Map key cannot, in turn, define collections.

Compositions can also be used as entity identifiers. This usage is covered in detail in ???

Again, compositions used as an entity identifier cannot, in turn, define collections.