Home How to fetch entities by objects value in JPA criteria api query
Reply: 0

How to fetch entities by objects value in JPA criteria api query

user1539
1#
user1539 Published in May 22, 2018, 12:09 am

I am using JPA with JSF datatable with lazy loading. Here One car can be owned by many users. So when i logged in to the application i want the cars which is owned by the user logged in(assume it as userId=1). I have a mapping table "Cars_User" that contains carId and userId columns. My Entities are like this My Car Class

@Entity
@Table(name="car")
public class Car implements Serializable {
 private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private String id;

 @Transient
 private boolean myCar;

 @NotNull
 @Size(min = 1, max = 50)
 public String name;

 @OneToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY, orphanRemoval = true)
 @JoinTable(name = "Cars_User", joinColumns = @JoinColumn(name = "carId"), inverseJoinColumns = @JoinColumn(name = "userId"))
 private List<User> carUsers = new ArrayList<User>();

 getters ...

 setters ...
}

User Class

@Entity(name = "User")
public class User implements Serializable {

   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;

   private String firstName;

   private String lastName;
}

I have found one answer for Lists of String collection in this link but how can be achieved in my case. I wanted to do get all Cars entities in criteria api that contains the logged in user id "userId" in carUsers Lists. can anyone please help?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.397413 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO