简体   繁体   English

JPQL ManyToMany选择

[英]JPQL ManyToMany select

I have a Many To Many relationship between two entities called: Car and Dealership. 我在两个实体之间有一个多对多的关系:汽车和经销商。

In native MySQL I have: 在原生MySQL中我有:

car (id and other values)
dealership (id and other values)
car_dealership  (car_id and dealership_id)

And the query I want to make in JPQL is: 我想在JPQL中创建的查询是:

#Select List of cars in multiple dealerships
SELECT car_id FROM car_dealership WHERE dealership_id IN(1,2,3,56,78,999);

What is the proper way to make the JPQL equivalent? 使JPQL等效的正确方法是什么?

My Java method signature is: 我的Java方法签名是:

public List<Car> findByDealership(List<Dealership> dealerships);

I have tried 我努力了

    //TOTALLY WRONG QUERY CALL!!!     
    Query query = em.createQuery("SELECT c FROM Car c WHERE :dealer_ids IN c.dealerships");
    List<Long> dealerIds = new ArrayList<Long>();
    for(Dealership d : dealerships) {
        dealerIds.add(d.getId());
    }
    query.setParameter(":dealer_ids", dealerIds); 
    List<Dealership> result = (List<Dealership>) query.getResultList();
    return result;
}

Here is my JPA Annotations for such relationship in java: 这是我在java中的这种关系的JPA注释:

@Entity
@Table(name = "car")
public class Car implements Serializable {

     //Setup of values and whatnot....
     @ManyToMany
     @JoinTable(name = "car_dealership", joinColumns =
     @JoinColumn(name = "car_id", referencedColumnName = "id"),
     inverseJoinColumns = @JoinColumn(name = "dealership_id", referencedColumnName = "id"))
     private List<Dealership> dealerships;

     ... other stuff (getters/setters)

}

@Entity
@Table(name = "property")
public class Dealership implements Serializable {

    //Setting of values and whatnot

    @ManyToMany(mappedBy = "dealerships")
    private List<Car> cars;

    .... other stuff(getters/setters)

}

EDIT 编辑

I also have tried: 我也尝试过:

 Query query = em.createQuery("SELECT c FROM Car c INNER JOIN c.dealerships d WHERE d IN (:deals)");
 query.setParameter("deals", dealerships);

Which threw the Error: 哪个扔了错误:

org.eclipse.persistence.exceptions.QueryException

Exception Description: Object comparisons can only use the equal() or notEqual() operators.  
Other comparisons must be done through query keys or direct attribute level comparisons. 

Expression: [
Relation operator [ IN ]
Query Key dealerships
  Base stackoverflow.question.Car
Constant [
Parameter deals]]
select car from Car car 
inner join car.dealerships dealership
where dealership in :dealerships

The parameter must be a collection of Dealership instances. 该参数必须是经销商实例的集合。

If you want to use a collection of dealership IDs, use 如果您想使用经销商ID的集合,请使用

select car from Car car 
inner join car.dealerships dealership
where dealership.id in :dealershipIds

Remamber that JPQL always use entities, mapped attributes and associations. 请记住,JPQL始终使用实体,映射属性和关联。 Never table and column names. 从不使用表名和列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM