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?