Matthias Andreas Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 1 | package eu.mulk.demos.blog; |
| 2 | |
| 3 | import java.util.List; |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 4 | import java.util.Set; |
Matthias Andreas Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 5 | import javax.transaction.Transactional; |
| 6 | import javax.ws.rs.GET; |
| 7 | import javax.ws.rs.Path; |
| 8 | import javax.ws.rs.Produces; |
| 9 | import javax.ws.rs.core.MediaType; |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 10 | import org.hibernate.annotations.LazyToOne; |
| 11 | import org.hibernate.annotations.LazyToOneOption; |
| 12 | import org.jboss.logging.Logger; |
Matthias Andreas Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 13 | |
| 14 | @Path("/posts") |
| 15 | public class PostResource { |
| 16 | |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 17 | 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 Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 24 | @GET |
| 25 | @Produces(MediaType.TEXT_PLAIN) |
| 26 | @Transactional |
| 27 | public List<Post> getAll() { |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 28 | clearLog(); |
| 29 | |
Matthias Andreas Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 30 | return Post.findAll().list(); |
| 31 | } |
| 32 | |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 33 | /** |
| 34 | * Fetches all posts with comments included. |
| 35 | * |
| 36 | * Lazy fetching. Simple. No surprises. |
| 37 | */ |
Matthias Andreas Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 38 | @GET |
| 39 | @Produces(MediaType.TEXT_PLAIN) |
| 40 | @Transactional |
| 41 | @Path("/q1") |
| 42 | public List<Post> getAllWithComments() { |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 43 | clearLog(); |
| 44 | |
| 45 | return Post.find( |
| 46 | """ |
| 47 | SELECT p FROM Post p |
| 48 | LEFT JOIN FETCH p.comments |
| 49 | """) |
| 50 | .list(); |
Matthias Andreas Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 53 | /** |
| 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 Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 60 | @GET |
| 61 | @Produces(MediaType.TEXT_PLAIN) |
| 62 | @Transactional |
| 63 | @Path("/q2") |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 64 | 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 Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Matthias Andreas Benkard | 0754057 | 2021-02-27 07:11:36 +0100 | [diff] [blame^] | 75 | /** |
| 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 Benkard | 0c009e8 | 2021-02-26 07:23:27 +0100 | [diff] [blame] | 138 | } |