Chapter III SpringBoot - Part 3 - Entity
Chapter III SpringBoot - Part 3 - Entity
§ Entity in JPA is a plain Java class representing data that can be persisted in the
database.
2
SPRING DATA JPA / ENTITIES
These are the properties of an entity that an object must have:
§ Persistability:
An object is called persistent if it is stored in the database and can be
accessed anytime.
§ Persistent Identity: each entity is unique and represents as an object identity.
Similarly, when the object identity is stored in a database then it is represented as
persistence identity. This object identity is equivalent to primary key in database.
§ Transactionality: Entity can perform various operations such as create, delete, update.
Each operation makes some changes in the database.
§ Granuality: Entities should not be primitives, primitive wrappers or built-in objects
with single dimensional state.
3
SPRING DATA JPA
ENTITIES
4
SPRING DATA JPA / ENTITIES
@Entity: This annotation marks the class as a JPA entity, indicating that it will be
mapped to a database table.
@Table: In JPA, you can specify the correspondent table of an entity, in a specific
schema using the @Table annotation.
@Entity
@Table(name = "users", schema = "mydatabase")
public class User {
// ...
}
This @table annotation in this example, specifies that the entity is associated with the
table named "users" in the schema "mydatabase". 5
SPRING DATA JPA / ENTITIES
@Id annotation: The @Id annotation is used in JPA to designate a field as the primary
key of an entity. It indicates that this field uniquely identifies each instance of the
entity.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
§ It's often the case that you want the primary key to be automatically generated.
§ The @GeneratedValue annotation, used in conjunction with @Id, specifies how the
primary key value should be generated.
6
SPRING DATA JPA / ENTITIES
@Column annotation: the @Column annotation in JPA allows you to specify additional
details about how a field should be mapped to a database column.
§ name = "email": This is the name of the column in the database table.
§ unique = true: This attribute indicates that the values in the email column must be
unique.
§ nullable = false: This attribute indicates that the email column must have a non-null
value.
§ length = 100: This attribute specifies the maximum length of the email field.
7
SPRING DATA JPA / ENTITIES
@Column(name = "birthdate")
@Temporal(TemporalType.DATE)
private Date birthdate;
@Transient
private Integer age;
§ @Transient: Marks the age property as transient, meaning it's calculated, not stored in
the database.
8
SPRING DATA JPA /
9
RELATIONSHIPS
BETWEEN ENTITIES
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
§ In the context of Spring Data JPA, relationships between entities refer to how different
entities in your application are associated with each other.
§ These relationships define how data in one entity can be related to data in another
entity.
§ There are several types of relationships that can exist between entities:
Many-to-Many Relationship
10
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
§ One-to-One Relationship: This is a relationship where one entity is associated with
exactly one instance of another entity. For example, a User may have one Address and
vice versa.
§ In Spring Data JPA, @OneToOne annotation is used for defining one-to-one relationship.
11
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using a Foreign Key:
In a one-to-one relationship using a foreign key, one entity is associated with exactly one
instance of another entity, and this relationship is established by using a foreign key
column in one of the entities.
12
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using a Foreign Key
13
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using a Foreign Key:
§ In the User entity, we have an address property of type Address. This represents the
one-to-one relationship.
§ In this case, it's named address_id and it references the id column in the address
table.
14
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using Shared Primary Key:
In a one-to-one relationship with a shared primary key, both entities share the same
primary key value. This means that the primary key of the dependent entity is also a
foreign key that references the primary key of the owning entity.
15
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using Shared Primary Key:
16
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using Shared Primary Key:
§ In
the User entity, the id field is generated as usual. It serves as both the primary key
and the foreign key for the Address entity.
§ The@OneToOne annotation with mappedBy is used to indicate that the relationship is
defined by the user property in the Address entity.
§ @PrimaryKeyJoinColumn is used to specify that the primary key of the User entity will
also serve as the primary key of the Address entity.
§ Inthe Address entity, the id field is used as both the primary key and the foreign key
that references the User entity.
§ @MapsId is used to indicate that this entity shares its primary key with the User entity.
§ @JoinColumn is used to specify the foreign key column. In this case, it's named id and it
refers to the primary key of the User entity. 17
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using a Join Table:
In a one-to-one relationship using a join table, an additional table is introduced to
manage the relationship between the two entities. This join table contains foreign key
references to both entities.
18
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using a Join Table:
19
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToOne relationship using a Join Table:
§ @JoinTable: This annotation specifies the details of the join table.
§ joinColumns: This defines the foreign key column(s) from the owning side (User entity)
in the join table.
§ user_id references the id column in the User entity.
§ inverseJoinColumns: This defines the foreign key column(s) from the inverse side
(Address entity) in the join table.
§ address_id references the id column in the Address entity.
20
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToMany and ManyToOne relationship :
In a one-to-many relationship, one entity can be associated with multiple instances of
another entity. In this example, one User can have multiple Addresses.
21
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToMany and ManyToOne relationship :
22
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
OneToMany and ManyToOne relationship :
§ Inthe User entity, we have added a list of Addresses. This represents the one-to-many
relationship.
§ @OneToMany annotation is used to indicate the type of relationship. The mappedBy
attribute specifies that the relationship is defined by the user property in the Address
entity.
§ Inthe Address entity, we have a reference to a User. This represents the many-to-one
side of the relationship.
§ @ManyToOne annotation is used to indicate the type of relationship. The @JoinColumn
annotation specifies the foreign key column, which in this case is user_id referencing
the id column in the User entity.
23
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
ManyToMany relationship :
In a many-to-many relationship, both entities can be associated with multiple instances
of the other entity. For example, in a User and Role relationship, a single user can have
multiple roles, and a single role can be associated with multiple users.
24
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
ManyToMany relationship :
25
SPRING DATA JPA / RELATIONSHIPS BETWEEN ENTITIES
ManyToMany relationship :
§ In the User entity, we have a Set of Roles. This represents the many-to-many
relationship.
§ @ManyToMany annotation is used to indicate the type of relationship.
§ Inthe Role entity, we have a Set of Users. This represents the inverse side of the many-
to-many relationship.
§ @ManyToMany annotation is used again, but this time with mappedBy to indicate that
the relationship is defined by the roles property in the User entity.
26