0% found this document useful (0 votes)
45 views21 pages

Session 5 - Hibernate - Advanced Mappings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views21 pages

Session 5 - Hibernate - Advanced Mappings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

HIBERNATE

ADVANCED MAPPINGS
HIBERNATE – Advanced Mappings
• One-to-One Mapping:
Shared Primary Key
– In a one-to-one association, you can use a
shared primary key, where the primary key of
the associated entity is the same as the
primary key of the main entity.
– This can be achieved using
@PrimaryKeyJoinColumn or @JoinColumn
annotations.
• One-to-Many and Many-to-One Mapping:

Bidirectional Association:
– In a bidirectional one-to-many/many-to-one
relationship, you can use the @OneToMany
and @ManyToOne annotations to establish
the association between two entities.
– Make sure to use mappedBy attribute in
@OneToMany to indicate the reverse side of
the association.
• Many-to-Many Mapping:

Bidirectional Association:
– In a many-to-many relationship, you can
use the @ManyToMany annotation to
associate two entities.
– Similar to one-to-many, you should use
the mappedBy attribute in
@ManyToMany to specify the reverse side
of the association.
• Embedded Objects:

@Embeddable and @Embedded:


– You can use these annotations to
represent complex or value-type objects
that are embedded within an entity.
– The @Embedded annotation is used to
mark the field that holds the embedded
object, while @Embeddable is used on the
class representing the embedded object.
• Inheritance Mapping:
– Hibernate supports inheritance mapping,
allowing you to represent inheritance
hierarchies using different strategies like
Single Table, Table per Class, and Joined
Table strategies.
– Use @Inheritance and
@DiscriminatorColumn annotations for
specifying the inheritance strategy.
• Enumerated Types:
– You can map Java enums to database
columns using the @Enumerated annotation.
– This allows you to persist enum values as
strings or integers in the database.

• Collections of Value Types:


– You can use @ElementCollection to map
collections of value types (embeddable objects)
to separate tables.
• Association Table Mappings:
• For complex many-to-many
relationships, you can use an
association table to store additional
information about the association
using @JoinTable annotation.
Example - one to one mapping
• Example of two entities: Person and
Passport.
• Each Person has one Passport,
representing a one-to-one
relationship.
Person entity class:
import javax.persistence.*;
@Entity
@Table(name = "persons")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "person_id")
private Long id;
@Column(name = "name")
private String name;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
private Passport passport;

// Constructors, getters, setters, and other properties


// ...

}
Passport entity class:

import javax.persistence.*;
@Entity
@Table(name = "passports")
public class Passport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "passport_id")
private Long id;
@Column(name = "number")
private String number;

@OneToOne
@JoinColumn(name = "person_id")
private Person person;
// Constructors, getters, setters, and other properties
// ...

}
Annotations and Configurations:

• @Entity: Marks the class as a JPA entity, which


will be mapped to a database table.

• @Table: Specifies the name of the database


table corresponding to the entity.

• @Id: Marks the primary key field of the entity.

• @GeneratedValue: Indicates that the value for


the primary key is automatically generated
(using the identity strategy in this example).
Annotations and Configurations:
• @Column: Specifies the mapping between
the field and the corresponding database
column.
• @OneToOne: Specifies a one-to-one
association between Person and Passport.
• @JoinColumn: Specifies the foreign key
column in the passports table, linking it
to the persons table.
• The cascade attribute in the @OneToOne annotation
is set to CascadeType.ALL.

• This means that when you persist, update, or delete


a Person, the same operation will be cascaded to its
associated Passport.

• This ensures that the relationship remains


consistent.

• With these mappings, Hibernate will handle the one-


to-one relationship between Person and Passport and
persist them into the corresponding database tables
accordingly.
Example – One to Many Relationship

• Hibernate mappings using a simple scenario with


two entities: Author and Book.

• Each Author can have multiple Book entities,


representing a one-to-many relationship.

• We'll also use embedded objects for the Address of


the authors.

• Ensure the necessary Hibernate and database


configurations are set up
Author entity class: java
import javax.persistence.*;

@Entity
@Table(name = "authors")
public class Author {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "author_id")
private Long id;

@Column(name = "name")
private String name;

@Embedded
private Address address;

// Constructors, getters, setters, and other properties


// ...

}
Address embedded object class:
import javax.persistence.Embeddable;

@Embeddable
public class Address {

private String street;

private String city;

private String zipCode;

// Constructors, getters, setters, and other properties


// ...

}
Book entity class:
import javax.persistence.*;
@Entity
@Table(name = "books")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private Long id;

@Column(name = "title")
private String title;

@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
// Constructors, getters, setters, and other properties
// ...
}
Annotations and Configurations

• @Entity: Marks the class as a JPA entity, which will be


mapped to a database table.

• @Table: Specifies the name of the database table


corresponding to the entity.

• @Id: Marks the primary key field of the entity.

• @GeneratedValue: Indicates that the value for the primary


key is automatically generated (using the identity strategy in
this example).

• @Column: Specifies the mapping between the field and the


corresponding database column.
Annotations and Configurations

• @Embedded: Indicates that the Address object

will be embedded within the Author entity.

• @ManyToOne: Specifies a many-to-one

association between Book and Author.

• @JoinColumn: Specifies the foreign key

column in the books table, linking it to the

authors table.
• With these mappings, Hibernate will
handle the relationships between the
Author and Book entities and persist
them into the corresponding database
tables accordingly.

You might also like