Ігор Онек
Ігор Онек

Reputation: 270

How to get object from many to one relation, by some property

I have two entities:

@Entity
public class FuturePlan {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int fpId;
    private String about;
    private Date travelDate;
    @ManyToOne(fetch=FetchType.LAZY)
    private User user;
    @ManyToOne(fetch=FetchType.LAZY)
    private Location location;

    ....contructors and get/set

...and

@Entity
public class Location {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int lId;
    private String lName;
    ....contructors and get/set

How to get all futurePlan instances where location.name == "Something"?

Upvotes: 0

Views: 297

Answers (1)

shankarsh15
shankarsh15

Reputation: 1967

1.You can very well use native Query something like below:

 String q="select * from employer employer,employee where employer.id=
                    employee.employerId and employee.location='xyz'";
Query query= em.createNativeQuery(q,Object[].class);
List<Object[]> students= query.getResultList();
  1. You can also achieve the same results by using HQL as well.

  2. You can use Criteria Query too.

Upvotes: 1

Related Questions