77import java .sql .Timestamp ;
88import java .time .LocalDateTime ;
99import 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