Mapping Embeddable Components in Hibernate
Embeddable Component Mapping
An Address class is defined as embeddable without a corresponding database table.
package com.example.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
@Embeddable
public class Address {
@NotNull
@Column(nullable = false)
protected String street;
@NotNull
@Column(nullable = false)
protected String zipcode;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
Mapping an Embeddable Copmonent
package com.example.hibernate.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator = "id_generator")
protected long id;
protected String username;
// Embedded component
protected Address address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
The resulting database schema:
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL,
`street` VARCHAR(255) NOT NULL,
`zipcode` VARCHAR(255) NOT NULL,
`username` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
Overirding Column Names for Embeddable Components
package com.example.hibernate.entity;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator = "id_generator")
protected long id;
protected String username;
// Embedded component
protected Address address;
@AttributeOverrides({
@AttributeOverride(
name = "street",
column = @Column(name = "order_street")
),
@AttributeOverride(
name = "zipcode",
column = @Column(name = "order_zipcode")
)
})
protected Address orderAddress;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getOrderAddress() {
return orderAddress;
}
public void setOrderAddress(Address orderAddress) {
this.orderAddress = orderAddress;
}
}
Resulting table schema:
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL,
`street` VARCHAR(255) NOT NULL,
`zipcode` VARCHAR(255) NOT NULL,
`order_street` VARCHAR(255) NULL DEFAULT NULL,
`order_zipcode` VARCHAR(255) NULL DEFAULT NULL,
`username` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
Nested Embeddable Components
The Address class includes a nested City component.
package com.example.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
@Embeddable
public class Address {
@NotNull
@Column(nullable = false)
protected String street;
@NotNull
@Column(nullable = false)
protected String zipcode;
@NotNull
protected City city;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
The nested City component:
package com.example.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
@Embeddable
public class City {
@NotNull
@Column(nullable = false)
protected String country;
@NotNull
@Column(nullable = false)
protected String name;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User entity mapping:
package com.example.hibernate.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator = "id_generator")
protected long id;
protected String username;
// Embedded component
protected Address address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Generated table structure:
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`street` VARCHAR(255) NOT NULL,
`zipcode` VARCHAR(255) NOT NULL,
`username` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
Nested structures can theoretically be extended to any depth.