Spring Data JPA Specification with MayToMany relation










0















I'm trying to use Specification to build a search criteria with Spring Data JPA.



I have 2 entities



@Entity
public class User {
@Id
private Long userId;

@ManyToMany
@JoinColumn(name="locationId")
@JsonIgnore
private List<Location> locations;

//getters and setters


And Location



public class Location {

@Id
private long locationId;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH,
CascadeType.REFRESH , mappedBy = "locations")
@JsonIgnore
private List<User> users;

//getters and setters


Now I would like to find all users with a given list of locations, so I'm trying to build the predicate.



My problem is getting the relation table between the 2 entities... I'm trying in this way



Root<Location> second = query.from(Location.class);
Expression<Collection<Root>> userLocations = second.get("users");
return builder.and(builder.isMember(root, userLocations));


This is the sql query i need



select distinct * from User u 
join User_Location ul on u.userId = ul.`users_userId`
where ul.`locations_locationId` in (1,2,3...)


The main difficult for me is telling Spring to use the relation table User_Location which is not an entity...



But I indeed miss something...










share|improve this question
























  • You can break down a bidirectional Many-to-Many relationship to two One-To-Many relationships. The config can be found here.

    – huytmb
    Nov 13 '18 at 8:52






  • 1





    You don't need to use the join table in your query. Simply do "select distrinct u from User u where u.locations in (l1, l2, l3, …)" In criteriaBuilder the class is a "Join" class, so Join<User, Location> locations = root.join("locations)" if I remember correctly. Then "query.where( builder.in(locations, locationList))". Something along those lines.

    – K.Nicholas
    Nov 13 '18 at 20:00











  • thanks, this works perfectly

    – besmart
    Nov 14 '18 at 13:01















0















I'm trying to use Specification to build a search criteria with Spring Data JPA.



I have 2 entities



@Entity
public class User {
@Id
private Long userId;

@ManyToMany
@JoinColumn(name="locationId")
@JsonIgnore
private List<Location> locations;

//getters and setters


And Location



public class Location {

@Id
private long locationId;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH,
CascadeType.REFRESH , mappedBy = "locations")
@JsonIgnore
private List<User> users;

//getters and setters


Now I would like to find all users with a given list of locations, so I'm trying to build the predicate.



My problem is getting the relation table between the 2 entities... I'm trying in this way



Root<Location> second = query.from(Location.class);
Expression<Collection<Root>> userLocations = second.get("users");
return builder.and(builder.isMember(root, userLocations));


This is the sql query i need



select distinct * from User u 
join User_Location ul on u.userId = ul.`users_userId`
where ul.`locations_locationId` in (1,2,3...)


The main difficult for me is telling Spring to use the relation table User_Location which is not an entity...



But I indeed miss something...










share|improve this question
























  • You can break down a bidirectional Many-to-Many relationship to two One-To-Many relationships. The config can be found here.

    – huytmb
    Nov 13 '18 at 8:52






  • 1





    You don't need to use the join table in your query. Simply do "select distrinct u from User u where u.locations in (l1, l2, l3, …)" In criteriaBuilder the class is a "Join" class, so Join<User, Location> locations = root.join("locations)" if I remember correctly. Then "query.where( builder.in(locations, locationList))". Something along those lines.

    – K.Nicholas
    Nov 13 '18 at 20:00











  • thanks, this works perfectly

    – besmart
    Nov 14 '18 at 13:01













0












0








0








I'm trying to use Specification to build a search criteria with Spring Data JPA.



I have 2 entities



@Entity
public class User {
@Id
private Long userId;

@ManyToMany
@JoinColumn(name="locationId")
@JsonIgnore
private List<Location> locations;

//getters and setters


And Location



public class Location {

@Id
private long locationId;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH,
CascadeType.REFRESH , mappedBy = "locations")
@JsonIgnore
private List<User> users;

//getters and setters


Now I would like to find all users with a given list of locations, so I'm trying to build the predicate.



My problem is getting the relation table between the 2 entities... I'm trying in this way



Root<Location> second = query.from(Location.class);
Expression<Collection<Root>> userLocations = second.get("users");
return builder.and(builder.isMember(root, userLocations));


This is the sql query i need



select distinct * from User u 
join User_Location ul on u.userId = ul.`users_userId`
where ul.`locations_locationId` in (1,2,3...)


The main difficult for me is telling Spring to use the relation table User_Location which is not an entity...



But I indeed miss something...










share|improve this question
















I'm trying to use Specification to build a search criteria with Spring Data JPA.



I have 2 entities



@Entity
public class User {
@Id
private Long userId;

@ManyToMany
@JoinColumn(name="locationId")
@JsonIgnore
private List<Location> locations;

//getters and setters


And Location



public class Location {

@Id
private long locationId;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH,
CascadeType.REFRESH , mappedBy = "locations")
@JsonIgnore
private List<User> users;

//getters and setters


Now I would like to find all users with a given list of locations, so I'm trying to build the predicate.



My problem is getting the relation table between the 2 entities... I'm trying in this way



Root<Location> second = query.from(Location.class);
Expression<Collection<Root>> userLocations = second.get("users");
return builder.and(builder.isMember(root, userLocations));


This is the sql query i need



select distinct * from User u 
join User_Location ul on u.userId = ul.`users_userId`
where ul.`locations_locationId` in (1,2,3...)


The main difficult for me is telling Spring to use the relation table User_Location which is not an entity...



But I indeed miss something...







java jpa spring-data-jpa many-to-many criteria-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 8:28







besmart

















asked Nov 12 '18 at 21:45









besmartbesmart

66222455




66222455












  • You can break down a bidirectional Many-to-Many relationship to two One-To-Many relationships. The config can be found here.

    – huytmb
    Nov 13 '18 at 8:52






  • 1





    You don't need to use the join table in your query. Simply do "select distrinct u from User u where u.locations in (l1, l2, l3, …)" In criteriaBuilder the class is a "Join" class, so Join<User, Location> locations = root.join("locations)" if I remember correctly. Then "query.where( builder.in(locations, locationList))". Something along those lines.

    – K.Nicholas
    Nov 13 '18 at 20:00











  • thanks, this works perfectly

    – besmart
    Nov 14 '18 at 13:01

















  • You can break down a bidirectional Many-to-Many relationship to two One-To-Many relationships. The config can be found here.

    – huytmb
    Nov 13 '18 at 8:52






  • 1





    You don't need to use the join table in your query. Simply do "select distrinct u from User u where u.locations in (l1, l2, l3, …)" In criteriaBuilder the class is a "Join" class, so Join<User, Location> locations = root.join("locations)" if I remember correctly. Then "query.where( builder.in(locations, locationList))". Something along those lines.

    – K.Nicholas
    Nov 13 '18 at 20:00











  • thanks, this works perfectly

    – besmart
    Nov 14 '18 at 13:01
















You can break down a bidirectional Many-to-Many relationship to two One-To-Many relationships. The config can be found here.

– huytmb
Nov 13 '18 at 8:52





You can break down a bidirectional Many-to-Many relationship to two One-To-Many relationships. The config can be found here.

– huytmb
Nov 13 '18 at 8:52




1




1





You don't need to use the join table in your query. Simply do "select distrinct u from User u where u.locations in (l1, l2, l3, …)" In criteriaBuilder the class is a "Join" class, so Join<User, Location> locations = root.join("locations)" if I remember correctly. Then "query.where( builder.in(locations, locationList))". Something along those lines.

– K.Nicholas
Nov 13 '18 at 20:00





You don't need to use the join table in your query. Simply do "select distrinct u from User u where u.locations in (l1, l2, l3, …)" In criteriaBuilder the class is a "Join" class, so Join<User, Location> locations = root.join("locations)" if I remember correctly. Then "query.where( builder.in(locations, locationList))". Something along those lines.

– K.Nicholas
Nov 13 '18 at 20:00













thanks, this works perfectly

– besmart
Nov 14 '18 at 13:01





thanks, this works perfectly

– besmart
Nov 14 '18 at 13:01












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270564%2fspring-data-jpa-specification-with-maytomany-relation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270564%2fspring-data-jpa-specification-with-maytomany-relation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo