Entity Introduction#
What Is The Entity?#
Entity
objects: An entity is a simple Java class that represents a row in a database table.Entities
can be concrete classes or abstract classes. They maintain states by using properties or fields.- So in
Entity
classes we will use a lot of mapping annotations from JPA, you can review these annotations in the table below:
Annotation | Description |
---|---|
@Entity | The @Entity annotation is used to specify that the currently annotated class represents an entity type. Unlike basic and embeddable types, entity types have an identity and their state is managed by the underlying Persistence Context. |
@Table | The @Table annotation is used to specify the primary table of the currently annotated entity. |
@Id | The @Id annotation specifies the entity identifier. An entity must always have an identifier attribute which is used when loading the entity in a given Persistence Context. |
@GeneratedValue | The @GeneratedValue annotation specifies that the entity identifier value is automatically generated using an identity column, a database sequence, or a table generator. Hibernate supports the @GeneratedValue mapping even for UUID identifiers. |
@Type | The @Type annotation is used to specify the Hibernate @Type used by the currently annotated basic attribute. |
@Column | The @Column annotation is used to specify the mapping between a basic entity attribute and the database table column. |
@Enumerated | The @Enumerated annotation is used to specify that an entity attribute represents an enumerated type. In which, ORDINAL: stored according to the enum value’s ordinal position within the enum class, as indicated by java.lang.Enum#ordinal STRING: stored according to the enum value’s name, as indicated by java.lang.Enum#name |
@OneToMany | The @OneToMany annotation is used to specify a one-to-many database relationship. |
@ManyToOne | The @ManyToOne annotation is used to specify a many-to-one database relationship. |
@JoinColumn | The @JoinColumn annotation is used to specify the FOREIGN KEY column used when joining an entity association or an embeddable collection. |
@PrePersist | The @PrePersist annotation is used to specify a callback method that fires before an entity is persisted. |
@PreUpdate | The @PreUpdate annotation is used to specify a callback method that fires before an entity is updated. |
Entity Example#
- We will create entities which represent for 3 tables in the database.
- Note: You don't need to create these 3 tables in the database because JPA will create for you automatically when the Spring Boot application has started.
- So in Entity classes we will use a lot of mapping annotations from JPA, so you can review these annotations in the table below:
Annotation | Description |
---|---|
@Entity | The @Entity annotation is used to specify that the currently annotated class represents an entity type. Unlike basic and embeddable types, entity types have an identity and their state is managed by the underlying Persistence Context. |
@Table | The @Table annotation is used to specify the primary table of the currently annotated entity. |
@Id | The @Id annotation specifies the entity identifier. An entity must always have an identifier attribute which is used when loading the entity in a given Persistence Context. |
@GeneratedValue | The @GeneratedValue annotation specifies that the entity identifier value is automatically generated using an identity column, a database sequence, or a table generator. Hibernate supports the @GeneratedValue mapping even for UUID identifiers. |
@Type | The @Type annotation is used to specify the Hibernate @Type used by the currently annotated basic attribute. |
@Column | The @Column annotation is used to specify the mapping between a basic entity attribute and the database table column. |
@Enumerated | The @Enumerated annotation is used to specify that an entity attribute represents an enumerated type. In which, ORDINAL: stored according to the enum value’s ordinal position within the enum class, as indicated by java.lang.Enum#ordinal STRING: stored according to the enum value’s name, as indicated by java.lang.Enum#name |
@OneToMany | The @OneToMany annotation is used to specify a one-to-many database relationship. |
@ManyToOne | The @ManyToOne annotation is used to specify a many-to-one database relationship. |
@JoinColumn | The @JoinColumn annotation is used to specify the FOREIGN KEY column used when joining an entity association or an embeddable collection. |
@PrePersist | The @PrePersist annotation is used to specify a callback method that fires before an entity is persisted. |
@PreUpdate | The @PreUpdate annotation is used to specify a callback method that fires before an entity is updated. |
-
The fist Entity is
CustomerEntity
, this Entity will be the parent ofOrderEntity
and oneCustomerEntity
will have manyOrderEntity
. TheCustomerEntity
java class will look like the code below.
CustomerEntity.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
- Next we will create
OrderEntity
, so this Entity will be the parent ofItemEntity
and OneOrderEntity
will have manyItemEntity
. TheOrderEntity
java class will be look like as below
OrderEntity.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
- Next we will create
ItemEntity
. TheOrderEntity
java class will be look like as below
ItemEntity.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
- So that's all for creating Entities and relationships.