Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
Hibernate allows us to persist collections in a Java application, such as lists, sets, and maps. When we talk about "Collection Mapping" in Hibernate, we're referring to the ability to map these Java collections to relational database structures, such as tables.
Let's delve into how you can map Java collections in Hibernate:
Assume you have a Person
class with a list of PhoneNumber
objects.
@Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "person_id") private List<PhoneNumber> phoneNumbers; // getters, setters, constructors }
In the above mapping, the @OneToMany
annotation is used to map a one-to-many relationship between Person
and PhoneNumber
. The @JoinColumn
specifies the foreign key column in the PhoneNumber
table.
If you want to ensure that phone numbers are unique for a person, you can use a Set
:
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "person_id") private Set<PhoneNumber> phoneNumbers;
If each phone number is associated with a type (like 'home', 'work'), you can use a Map
:
@Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ElementCollection @CollectionTable(name = "phone_numbers", joinColumns = @JoinColumn(name = "person_id")) @MapKeyColumn(name = "phone_type") @Column(name = "phone_number") private Map<String, String> phoneNumbers; // getters, setters, constructors }
In this example, @ElementCollection
is used to specify a collection of basic values, and @CollectionTable
is used to define the table that will hold the collection.
Hibernate also allows you to maintain a sorted collection. For example, to maintain a sorted list of phone numbers:
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "person_id") @OrderColumn(name = "sequence") private List<PhoneNumber> phoneNumbers;
The @OrderColumn
annotation specifies the column in the PhoneNumber
table that Hibernate will use to maintain the order of the list.
Lazy Loading: By default, collections are lazily loaded in Hibernate, which means the collection data isn't fetched from the database until you access the collection. You can change this behavior by setting fetch = FetchType.EAGER
on your collection mapping.
Cascade Options: The cascade
attribute allows you to specify cascading options. For example, CascadeType.ALL
means operations like persist, merge, remove, etc., will cascade from Person
to PhoneNumber
.
Inverse Side: If you want to establish a bidirectional relationship, decide on the inverse side (the side that doesn't own the relationship) using the mappedBy
attribute. The inverse side is crucial in bidirectional relationships for ORM to know which side has the responsibility to manage the relationship.
Remember, Hibernate abstracts the complexities of managing these relationships, but it's still vital for developers to understand the underlying SQL operations that Hibernate performs. This ensures efficient use of the framework and helps in performance tuning. Ensure you test your mappings and understand the SQL generated by Hibernate, especially when working with collections and relationships.
Hibernate collection mapping example:
Mapping collections in Hibernate involves associating a collection of elements (e.g., List, Set, Map) with an entity.
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @OneToMany(mappedBy = "department") private List<Employee> employees; // Other properties and methods }
Mapping collections in Hibernate entities:
Map collections in Hibernate entities using annotations like @OneToMany
, @ManyToMany
, or XML configurations.
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @OneToMany(mappedBy = "department") private List<Employee> employees; // Other properties and methods }
Bag, list, set, and map mapping in Hibernate:
Choose appropriate collection types (Bag
, List
, Set
, Map
) based on the requirements of your application.
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @OneToMany(mappedBy = "department") private List<Employee> employees; // List mapping // Other properties and methods }
Hibernate ordered collection mapping:
Use @OrderColumn
to define the order of elements in an ordered collection.
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @OneToMany(mappedBy = "department") @OrderColumn(name = "employee_order") private List<Employee> employees; // Ordered List mapping // Other properties and methods }
Mapping collections of value types in Hibernate:
Map collections of value types using @ElementCollection
for more complex structures.
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @ElementCollection @CollectionTable(name = "phone_numbers", joinColumns = @JoinColumn(name = "department_id")) @Column(name = "phone_number") private Set<String> phoneNumbers; // Other properties and methods }
Hibernate collection mapping annotations:
Use annotations such as @OneToMany
, @ManyToMany
, @ElementCollection
, and others to map collections.
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @OneToMany(mappedBy = "department") private List<Employee> employees; // Other properties and methods }
Lazy loading collections in Hibernate:
Configure lazy loading for collections to load them only when accessed.
@OneToMany(mappedBy = "department", fetch = FetchType.LAZY) private List<Employee> employees;
Cascading operations in Hibernate collection mapping:
Define cascading operations for collections to propagate changes to associated entities.
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL) private List<Employee> employees;
Hibernate indexed collection mapping:
Use @MapKeyColumn
to map an indexed collection (e.g., Map).
@Entity @Table(name = "departments") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "department_id") private Long id; @OneToMany(mappedBy = "department") @MapKeyColumn(name = "employee_index") private Map<Integer, Employee> employees; // Indexed Map mapping // Other properties and methods }