[英]How to query list elements using spring JPA
Context: Salad contains multiple vegetables. 背景:沙拉含有多种蔬菜。 Lets assume that recipe of the salad is to add vegetables in the given order so the order also needs to be persisted
让我们假设沙拉的配方是按照给定的顺序添加蔬菜,因此订单也需要坚持
Here are the class definitions 这是类定义
public class Salad {
private String name;
private String description;
private List<Vegetable> veggies = new ArrayList<Vegetable>();
//setters and getters
}
public class Vegetable {
private String name;
private String description;
//setters and getters
}
Here is the hibernate-mapping.xml 这是hibernate-mapping.xml
<hibernate-mapping>
<class name="Salad" table="SALAD">
<id name="id">
<generator class="sequence"/>
</id>
<property name="name"/>
<property name="description"/>
<list name=veggies cascade="all-delete-orphan" table="SALAD_VEGGIES" lazy="false">
<key column="salad_id" />
<list-index column="idx" />
<many-to-many column="veg_id" class="Vegetable" />
</list>
</class>
<class name="Vegetable" table="VEGETABLE">
<id name="id">
<generator class="sequence"/>
</id>
<property name="name"/>
<property name="description"/>
</class>
</hibernate-mapping>
Objective: Create a JPQ query to fetch the list of all the vegetables involved for a given salad 目标:创建一个JPQ查询以获取给定沙拉所涉及的所有蔬菜的列表
What I tried? 我尝试了什么? 1)
1)
SELECT v FROM SALAD.veggies v where v.salad_id = :salad_id
which didn't work. 这没用。 How to query on a table that doesn't have a class (like List and Map? variables)
如何查询没有类的表(如List和Map?变量)
Try this: 尝试这个:
SELECT v FROM Salad s JOIN s.veggies v where s.id = :salad_id
Remember: in JPQL you work with Java fields, not with column names. 请记住:在JPQL中,您使用的是Java字段,而不是列名。 Also there is not relationship from
Vegetable
entity to the Salad
entity. 此外,还没有从
Vegetable
实体到Salad
实体的关系。
你甚至可以说
select v from Vegetable v where v.salad_id = :saladId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.