{"id":1333,"date":"2021-12-17T15:25:19","date_gmt":"2021-12-17T10:25:19","guid":{"rendered":"https:\/\/www.katebeckinsalegallery.com\/?p=1333"},"modified":"2021-12-27T15:33:02","modified_gmt":"2021-12-27T10:33:02","slug":"criteria-jpa-2-join-many-to-many-unidirectional-realtion","status":"publish","type":"post","link":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/","title":{"rendered":"Criteria JPA 2 join many to many unidirectional realtion"},"content":{"rendered":"

I have two entities, Movie and Genre, with a many to many relationship from genre to movie. Genre being the “parent” of the relationship.
\nThis generates three tables:\u00a0genre<\/code>,\u00a0movie<\/code>\u00a0and\u00a0genre_movie<\/code><\/p>\n

@Entity<\/span>\r\npublic<\/span> class<\/span> Genre<\/span> {\r\n  @Id<\/span>\r\n  @GeneratedValue(strategy = AUTO)<\/span>\r\n  private<\/span> Long id;\r\n\r\n  @ManyToMany(fetch = LAZY)<\/span>\r\n  @JoinTable( name = \"genre_movie\",\r\n    joinColumns = {@JoinColumn(name = \"genre_id\")},\r\n    inverseJoinColumns = {@JoinColumn(name = \"movie_id\")}\r\n  )<\/span>\r\n  private<\/span> Set<Movie> movies = new<\/span> HashSet<\/span><>();\r\n\r\n  ...\r\n}\r\n\r\n\r\n@Entity<\/span>\r\npublic<\/span> class<\/span> Movie<\/span> {\r\n  @Id<\/span>\r\n  @GeneratedValue(strategy = AUTO)<\/span>\r\n  private<\/span> Long id;\r\n  ...\r\n}\r\n<\/code><\/pre>\n

\"Entity<\/a><\/p>\n

I it possible perform this query using the criteria query api? Filter movies based on their genre id.<\/p>\n

select<\/span> *<\/span>\r\nfrom<\/span> movie\r\n  join<\/span> genre_movie on<\/span> movie.id =<\/span> genre_movie.movie_id\r\nwhere<\/span> genre_id =<\/span> 19<\/span>;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

I have two entities, Movie and Genre, with a many to many relationship from genre to movie. Genre being the “parent” of the relationship. This generates three tables:\u00a0genre,\u00a0movie\u00a0and\u00a0genre_movie @Entity public class Genre { @Id @GeneratedValue(strategy = AUTO) private Long id; Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[6925,6923,6921,6924,6922],"yoast_head":"\nCriteria JPA 2 join many to many unidirectional realtion - KateBeckin Sale Gallery<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Criteria JPA 2 join many to many unidirectional realtion - KateBeckin Sale Gallery\" \/>\n<meta property=\"og:description\" content=\"I have two entities, Movie and Genre, with a many to many relationship from genre to movie. Genre being the “parent” of the relationship. This generates three tables:\u00a0genre,\u00a0movie\u00a0and\u00a0genre_movie @Entity public class Genre { @Id @GeneratedValue(strategy = AUTO) private Long id; Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\" \/>\n<meta property=\"og:site_name\" content=\"KateBeckin Sale Gallery\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-17T10:25:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-27T10:33:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i.stack.imgur.com\/yzbHG.png\" \/>\n<meta name=\"author\" content=\"Steffy Alen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Steffy Alen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\"},\"author\":{\"name\":\"Steffy Alen\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/person\/1166481eb890b57fe129636d7da06053\"},\"headline\":\"Criteria JPA 2 join many to many unidirectional realtion\",\"datePublished\":\"2021-12-17T10:25:19+00:00\",\"dateModified\":\"2021-12-27T10:33:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\"},\"wordCount\":55,\"publisher\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i.stack.imgur.com\/yzbHG.png\",\"keywords\":[\"criteria-api\",\"hibernate\",\"java\",\"jpa\",\"sql\"],\"articleSection\":[\"More\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\",\"url\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\",\"name\":\"Criteria JPA 2 join many to many unidirectional realtion - KateBeckin Sale Gallery\",\"isPartOf\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i.stack.imgur.com\/yzbHG.png\",\"datePublished\":\"2021-12-17T10:25:19+00:00\",\"dateModified\":\"2021-12-27T10:33:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage\",\"url\":\"https:\/\/i.stack.imgur.com\/yzbHG.png\",\"contentUrl\":\"https:\/\/i.stack.imgur.com\/yzbHG.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.katebeckinsalegallery.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Criteria JPA 2 join many to many unidirectional realtion\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#website\",\"url\":\"https:\/\/www.katebeckinsalegallery.com\/\",\"name\":\"KateBeckin Sale Gallery\",\"description\":\"Do The Home Improvement\",\"publisher\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.katebeckinsalegallery.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#organization\",\"name\":\"KateBeckin Sale Gallery\",\"url\":\"https:\/\/www.katebeckinsalegallery.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.katebeckinsalegallery.com\/wp-content\/uploads\/2023\/06\/cropped-cropped-brown-road-and-yellow-house.-1.png\",\"contentUrl\":\"https:\/\/www.katebeckinsalegallery.com\/wp-content\/uploads\/2023\/06\/cropped-cropped-brown-road-and-yellow-house.-1.png\",\"width\":784,\"height\":437,\"caption\":\"KateBeckin Sale Gallery\"},\"image\":{\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/person\/1166481eb890b57fe129636d7da06053\",\"name\":\"Steffy Alen\",\"url\":\"https:\/\/www.katebeckinsalegallery.com\/author\/annatimar\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Criteria JPA 2 join many to many unidirectional realtion - KateBeckin Sale Gallery","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/","og_locale":"en_US","og_type":"article","og_title":"Criteria JPA 2 join many to many unidirectional realtion - KateBeckin Sale Gallery","og_description":"I have two entities, Movie and Genre, with a many to many relationship from genre to movie. Genre being the “parent” of the relationship. This generates three tables:\u00a0genre,\u00a0movie\u00a0and\u00a0genre_movie @Entity public class Genre { @Id @GeneratedValue(strategy = AUTO) private Long id; Read More","og_url":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/","og_site_name":"KateBeckin Sale Gallery","article_published_time":"2021-12-17T10:25:19+00:00","article_modified_time":"2021-12-27T10:33:02+00:00","og_image":[{"url":"https:\/\/i.stack.imgur.com\/yzbHG.png"}],"author":"Steffy Alen","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Steffy Alen","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#article","isPartOf":{"@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/"},"author":{"name":"Steffy Alen","@id":"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/person\/1166481eb890b57fe129636d7da06053"},"headline":"Criteria JPA 2 join many to many unidirectional realtion","datePublished":"2021-12-17T10:25:19+00:00","dateModified":"2021-12-27T10:33:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/"},"wordCount":55,"publisher":{"@id":"https:\/\/www.katebeckinsalegallery.com\/#organization"},"image":{"@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage"},"thumbnailUrl":"https:\/\/i.stack.imgur.com\/yzbHG.png","keywords":["criteria-api","hibernate","java","jpa","sql"],"articleSection":["More"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/","url":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/","name":"Criteria JPA 2 join many to many unidirectional realtion - KateBeckin Sale Gallery","isPartOf":{"@id":"https:\/\/www.katebeckinsalegallery.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage"},"image":{"@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage"},"thumbnailUrl":"https:\/\/i.stack.imgur.com\/yzbHG.png","datePublished":"2021-12-17T10:25:19+00:00","dateModified":"2021-12-27T10:33:02+00:00","breadcrumb":{"@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#primaryimage","url":"https:\/\/i.stack.imgur.com\/yzbHG.png","contentUrl":"https:\/\/i.stack.imgur.com\/yzbHG.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.katebeckinsalegallery.com\/criteria-jpa-2-join-many-to-many-unidirectional-realtion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.katebeckinsalegallery.com\/"},{"@type":"ListItem","position":2,"name":"Criteria JPA 2 join many to many unidirectional realtion"}]},{"@type":"WebSite","@id":"https:\/\/www.katebeckinsalegallery.com\/#website","url":"https:\/\/www.katebeckinsalegallery.com\/","name":"KateBeckin Sale Gallery","description":"Do The Home Improvement","publisher":{"@id":"https:\/\/www.katebeckinsalegallery.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.katebeckinsalegallery.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.katebeckinsalegallery.com\/#organization","name":"KateBeckin Sale Gallery","url":"https:\/\/www.katebeckinsalegallery.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.katebeckinsalegallery.com\/wp-content\/uploads\/2023\/06\/cropped-cropped-brown-road-and-yellow-house.-1.png","contentUrl":"https:\/\/www.katebeckinsalegallery.com\/wp-content\/uploads\/2023\/06\/cropped-cropped-brown-road-and-yellow-house.-1.png","width":784,"height":437,"caption":"KateBeckin Sale Gallery"},"image":{"@id":"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.katebeckinsalegallery.com\/#\/schema\/person\/1166481eb890b57fe129636d7da06053","name":"Steffy Alen","url":"https:\/\/www.katebeckinsalegallery.com\/author\/annatimar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/posts\/1333"}],"collection":[{"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/comments?post=1333"}],"version-history":[{"count":1,"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/posts\/1333\/revisions"}],"predecessor-version":[{"id":1334,"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/posts\/1333\/revisions\/1334"}],"wp:attachment":[{"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/media?parent=1333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/categories?post=1333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.katebeckinsalegallery.com\/wp-json\/wp\/v2\/tags?post=1333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}