blob: 532e8733f26f9c9d5d0acca335bf7aecfc3c3f2e [file] [log] [blame]
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +01001package eu.mulk.demos.blog;
2
3import java.util.List;
Matthias Andreas Benkard07540572021-02-27 07:11:36 +01004import java.util.Set;
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +01005import javax.transaction.Transactional;
6import javax.ws.rs.GET;
7import javax.ws.rs.Path;
8import javax.ws.rs.Produces;
9import javax.ws.rs.core.MediaType;
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010010import org.hibernate.annotations.LazyToOne;
11import org.hibernate.annotations.LazyToOneOption;
12import org.jboss.logging.Logger;
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010013
14@Path("/posts")
15public class PostResource {
16
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010017 static final Logger log = Logger.getLogger(PostResource.class);
18
19 /**
20 * Fetches all posts with no extra information.
21 *
22 * Simple. No surprises.
23 */
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010024 @GET
25 @Produces(MediaType.TEXT_PLAIN)
26 @Transactional
27 public List<Post> getAll() {
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010028 clearLog();
29
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010030 return Post.findAll().list();
31 }
32
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010033 /**
34 * Fetches all posts with comments included.
35 *
36 * Lazy fetching. Simple. No surprises.
37 */
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010038 @GET
39 @Produces(MediaType.TEXT_PLAIN)
40 @Transactional
41 @Path("/q1")
42 public List<Post> getAllWithComments() {
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010043 clearLog();
44
45 return Post.find(
46 """
47 SELECT p FROM Post p
48 LEFT JOIN FETCH p.comments
49 """)
50 .list();
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010051 }
52
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010053 /**
54 * Fetches all posts with author info included.
55 *
56 * <strong>Oops!</strong>
57 *
58 * {@link LazyToOne} with {@link LazyToOneOption#NO_PROXY} is needed to make this efficient.
59 */
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010060 @GET
61 @Produces(MediaType.TEXT_PLAIN)
62 @Transactional
63 @Path("/q2")
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010064 public List<Post> getAllWithAuthors() {
65 clearLog();
66
67 return Post.find(
68 """
69 SELECT p FROM Post p
70 LEFT JOIN FETCH p.author
71 """)
72 .list();
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +010073 }
74
Matthias Andreas Benkard07540572021-02-27 07:11:36 +010075 /**
76 * Fetches all posts with comments and category info included.
77 *
78 * <strong>Oops!</strong> Crashes.
79 *
80 * Either use {@link Set} and get bad performance or do it as in {@link
81 * #getAllWithCommentsAndCategories2()}.
82 */
83 @GET
84 @Produces(MediaType.TEXT_PLAIN)
85 @Transactional
86 @Path("/q3")
87 public List<Post> getAllWithCommentsAndCategories() {
88 clearLog();
89
90 return Post.find(
91 """
92 SELECT p FROM Post p
93 LEFT JOIN FETCH p.comments
94 LEFT JOIN FETCH p.categories
95 """)
96 .list();
97 }
98
99 /**
100 * Fetches all posts with comments and category info included.
101 *
102 * 2 queries, but hey, no cartesian explosion! Works really well.
103 */
104 @GET
105 @Produces(MediaType.TEXT_PLAIN)
106 @Transactional
107 @Path("/q4")
108 public List<Post> getAllWithCommentsAndCategories2() {
109 clearLog();
110
111 List<Post> posts = Post.find(
112 """
113 SELECT p FROM Post p
114 LEFT JOIN FETCH p.comments
115 """)
116 .list();
117
118 posts = Post.find(
119 """
120 SELECT DISTINCT p FROM Post p
121 LEFT JOIN FETCH p.categories
122 WHERE p IN (?1)
123 """,
124 posts)
125 .list();
126
127 return posts;
128 }
129
130 private static void clearLog() {
131 log.infof("""
132
133 -----------------------------------------------------
134 -------------------- NEW REQUEST --------------------
135 -----------------------------------------------------
136 """);
137 }
Matthias Andreas Benkard0c009e82021-02-26 07:23:27 +0100138}