Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
In Hibernate, as well as in the Java Persistence API (JPA), the @Embeddable
and @Embedded
annotations are used to represent and handle value types, which don't have their own identity. They allow you to map a group of columns in a single table to a single entity without needing a separate table for that embedded object.
Let's dive into these annotations and understand their usage.
Marks a class as being embeddable in an entity. This means the class can be stored as part of an entity, without having its own primary key and without being an entity by itself.
Suppose you have a User
entity and you want to embed an Address
class into it.
@Embeddable public class Address { private String street; private String city; private String state; private String zipCode; // getters, setters, constructors... }
The Address
class doesn't have its own primary key and is not an entity. It's just a value type that can be embedded in other entities.
Used to specify a property of an entity that is an instance of an embeddable class. When an entity field is annotated with @Embedded
, Hibernate will map the columns of the embedded object directly to the table of the entity containing it.
Continuing from the previous example, here's how you'd embed the Address
inside a User
entity:
@Entity public class User { @Id @GeneratedValue private Long id; private String username; @Embedded private Address address; // getters, setters, constructors... }
In the above example, the columns related to Address
(street
, city
, state
, zipCode
) will be directly mapped to the User
table, rather than creating a separate table for Address
.
If you wish to override the default column names of the embedded object, you can use the @AttributeOverrides
and @AttributeOverride
annotations.
Example:
@Entity public class User { @Id @GeneratedValue private Long id; private String username; @Embedded @AttributeOverrides({ @AttributeOverride(name="street", column=@Column(name="home_street")), @AttributeOverride(name="city", column=@Column(name="home_city")) }) private Address address; // ... }
In this example, instead of the default column name street
, the column will be named home_street
, and instead of city
, it will be home_city
.
The @Embeddable
and @Embedded
annotations in Hibernate and JPA offer a concise way to embed value types into entities without the overhead of managing a separate entity or table for them. It's especially useful for representing objects that don't have a meaningful existence or identity outside of the main entity they're contained in.
Hibernate @Embeddable
and @Embedded
example:
@Embeddable
is used to define a composite or embedded object, while @Embedded
is used to embed that object within an entity. This helps in creating a more complex entity with reusable components.@Embeddable public class Address { private String street; private String city; private String zipCode; // Getters and setters } @Entity public class Employee { @Embedded private Address address; // Other fields }
Using @EmbeddedId
in Hibernate entities:
@EmbeddedId
is used when the composite key is represented by an @Embeddable
class. It combines multiple attributes into a single composite key.@Embeddable public class EmployeeId { private String empId; private String departmentId; // Getters and setters } @Entity public class Employee { @EmbeddedId private EmployeeId employeeId; // Other fields }
Nested embeddables in Hibernate entities:
@Embeddable public class ContactDetails { private String phone; private String email; // Getters and setters } @Embeddable public class EmployeeDetails { private String firstName; private String lastName; @Embedded private ContactDetails contactDetails; // Getters and setters } @Entity public class Employee { @Id @GeneratedValue private Long id; @Embedded private EmployeeDetails employeeDetails; // Other fields }
Customizing mappings with @AttributeOverride
in Hibernate:
@AttributeOverride
is used to customize the mapping of attributes in an @Embeddable
class when it is embedded in an entity. It allows you to override the default column names.@Embeddable @AttributeOverride(name = "street", column = @Column(name = "home_street")) public class Address { private String street; private String city; private String zipCode; // Getters and setters } @Entity public class Employee { @Embedded private Address homeAddress; // Other fields }