Software Developer
In this example, you will learn how to map components using Hibernate Annotations. Consider the following relationship between Student and Address entity.
In this tutorial, we'll see how we can map one entity that contains embedded properties to a single database table. So, for this purpose, we'll use the @Embeddable and @Embedded annotations provided by the JPA.
Hibernate ORM 5.2.17 now allows you to use the @prepersist and @preupdate JPA entity listeners, we can simplify the previous example, as you may see in this article.
We want to encapsulate the audit logic in the Audit embeddable type:
@Embeddable
public class Audit {
@Column(name = "created_on")
private LocalDateTime createdOn;
@Column(name = "created_by")
private String createdBy;
@Column(name = "updated_on")
private LocalDateTime updatedOn;
@Column(name = "updated_by")
private String updatedBy;
@PrePersist
public void prePersist() {
createdOn = LocalDateTime.now();
createdBy = LoggedUser.get();
}
@PreUpdate
public void preUpdate() {
updatedOn = LocalDateTime.now();
updatedBy = LoggedUser.get();
}
//Getters and setters omitted for brevity
}
The JPA entities will use the Audit embeddable type as follows:
@Entity(name = "Post")
@Table(name = "post")
public class Post {
@Id
private Long id;
private String title;
@Embedded
private Audit audit = new Audit();
@ManyToMany
@JoinTable(
name = "post_tag",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private List<Tag> tags = new ArrayList<>();
//Getters and setters omitted for brevity
}
@Entity(name = "Tag")
@Table(name = "tag")
public class Tag {
@Id
private String name;
@Embedded
private Audit audit = new Audit();
//Getters and setters omitted for brevity
}
Now, when inserting 3 Tag entities:
Tag jdbc = new Tag();
jdbc.setName("JDBC");
entityManager.persist(jdbc);
Tag hibernate = new Tag();
hibernate.setName("Hibernate");
entityManager.persist(hibernate);
Tag jOOQ = new Tag();
jOOQ.setName("jOOQ");
entityManager.persist(jOOQ);
Hibernate properly sets the created_on and created_by columns on the associated tag rows:
INSERT INTO tag (
created_by,
created_on,
updated_by,
updated_on,
name
)
VALUES (
'Alice',
'2018-05-02 09:56:54.939',
NULL(VARCHAR),
NULL(TIMESTAMP),
'JDBC'
)
INSERT INTO tag (
created_by,
created_on,
updated_by,
updated_on,
name
)
VALUES (
'Alice',
'2018-05-02 09:56:54.955',
NULL(VARCHAR),
NULL(TIMESTAMP),
'Hibernate'
)
INSERT INTO tag (
created_by,
created_on,
updated_by,
updated_on,
name
)
VALUES (
'Alice',
'2018-05-02 09:56:54.955',
NULL(VARCHAR),
NULL(TIMESTAMP),
'jOOQ'
)
The same goes for the Post entity:
Post post = new Post();
post.setId(1L);
post.setTitle(
"High-Performance Java Persistence, 1st Edition
");
post.getTags().add(
entityManager.find(Tag.class, "JDBC")
);
post.getTags().add(
entityManager.find(Tag.class, "Hibernate")
);
post.getTags().add(
entityManager.find(Tag.class, "jOOQ")
);
entityManager.persist(post);
Hibernate generates the following INSERT statements:
INSERT INTO post (
created_by,
created_on,
updated_by,
updated_on,
title,
id
)
VALUES (
'Alice',
'2018-05-02 09:56:55.046',
NULL(VARCHAR),
NULL(TIMESTAMP),
'High-Performance Java Persistence, 1st Edition',
1
)
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 'JDBC')
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 'Hibernate')
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 'jOOQ')
When updating the Post entity:
Post post = entityManager.find(Post.class, 1L);
post.setTitle(
"High-Performance Java Persistence, 2nd Edition"
);
The updated_on and update_by columns will be set by the @PreUpdate event listener on the embeddable type:
UPDATE
post
SET
created_by = 'Alice',
created_on = '2018-05-02 09:56:55.046',
updated_by = 'Alice',
updated_on = '2018-05-02 09:56:55.106',
title = 'High-Performance Java Persistence, 2nd Edition'
WHERE
id = 1
So, even as previously, you could gain the same purpose the usage of an @entitylistener, now you apply the @prepersist and @preupdate event listeners son the embeddable type, consequently simplifying the implementation.