Skip to content

Commit c0bfb92

Browse files
committed
Update tests for course slides
1 parent 96e6ddf commit c0bfb92

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/inheritance/MappedSuperclassTest.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.sql.Timestamp;
88
import java.time.LocalDateTime;
99
import java.util.Date;
10+
import java.util.List;
11+
12+
import static org.junit.Assert.assertEquals;
1013

1114
/**
1215
* @author Vlad Mihalcea
@@ -62,13 +65,31 @@ public void test() {
6265
return post;
6366
});
6467

68+
doInJPA(entityManager -> {
69+
Board board = topic.getBoard();
70+
71+
List<Post> posts = entityManager
72+
.createQuery(
73+
"select p " +
74+
"from Post p " +
75+
"where p.board = :board", Post.class)
76+
.setParameter("board", board)
77+
.getResultList();
78+
});
79+
6580
doInJPA(entityManager -> {
6681
Long postId = topic.getId();
6782
LOGGER.info("Fetch statistics");
68-
PostStatistics statistics = entityManager
69-
.createQuery("select s from PostStatistics s join fetch s.topic t where t.id = :postId", PostStatistics.class)
70-
.setParameter("postId", postId)
71-
.getSingleResult();
83+
PostStatistics postStatistics = entityManager
84+
.createQuery(
85+
"select ps " +
86+
"from PostStatistics ps " +
87+
"join fetch ps.topic t " +
88+
"where t.id = :postId", PostStatistics.class)
89+
.setParameter("postId", postId)
90+
.getSingleResult();
91+
92+
assertEquals(postId, postStatistics.getTopic().getId());
7293
});
7394
}
7495

@@ -111,7 +132,7 @@ public static abstract class Topic {
111132
@Temporal(TemporalType.TIMESTAMP)
112133
private Date createdOn = new Date();
113134

114-
@ManyToOne
135+
@ManyToOne(fetch = FetchType.LAZY)
115136
private Board board;
116137

117138
public Long getId() {
@@ -213,8 +234,9 @@ public void incrementViews() {
213234
@Table(name = "post_statistics")
214235
public static class PostStatistics extends TopicStatistics<Post> {
215236

216-
@OneToOne
237+
@OneToOne(fetch = FetchType.LAZY)
217238
@MapsId
239+
@JoinColumn(name = "id")
218240
private Post topic;
219241

220242
public PostStatistics() {}
@@ -233,8 +255,9 @@ public Post getTopic() {
233255
@Table(name = "announcement_statistics")
234256
public static class AnnouncementStatistics extends TopicStatistics<Announcement> {
235257

236-
@OneToOne
258+
@OneToOne(fetch = FetchType.LAZY)
237259
@MapsId
260+
@JoinColumn(name = "id")
238261
private Announcement topic;
239262

240263
public AnnouncementStatistics() {}

0 commit comments

Comments
 (0)