问题描述
说明
有一个personrepository和person实体,person 类包含 list
there is a personrepository and person entity,
person class contains list
我尝试在自定义方法上添加 @query 注释并使用 jpql 获取结果,但 qualification 类字段在 jpql 中不可用于操作,因为它是存储库本身包含 list
i have tried to add @query annotation on custom method and use jpql to get the results, but qualification class fields were not available for manipulation in jpql as it repository itself contains list
如何通过这些 qualification 的嵌套字段进行搜索?
how can i search by these qualification's nested fields?
查询
现在我需要找到资格的 experienceinmonths 大于 3 且小于 9 且资格的名称字段 = 'java' 的人员实体列表.
now i need to find list of person entity where qualification's experienceinmonths is greater than 3 and less than 9 and qualification's name field = 'java'.
代码
person.java
person.java
@data @entity public class person { @id @generatedvalue(strategy = generationtype.identity) private string id; @notempty @size(min = 2) private string name; @notempty @size(min = 2) private string surname; @elementcollection(targetclass = java.util.arraylist.class, fetch = fetchtype.eager) private listqualifications = new arraylist<>(); }
personrepository.java
personrepository.java
@repository public interface personrepository extends jparepository{ }
qualification.java
qualification.java
@data @allargsconstructor public class qualification implements serializable { @id @generatedvalue private string id; private string name; private string experienceinmonths; }
不重复 这篇文章,因为这里是嵌套对象的集合.不仅仅是单一参考.
not duplicate of this post, as here is the collection of nested objects. not just single reference.
推荐答案
首先,将 experienceinmonths 从 string 改为 int (否则你可以不要将字符串与数字进行比较).然后你可以尝试使用这个香肠":
first, change experienceinmonths from string to int (otherwise you can not compare the string with the number). then you can try to use this 'sausage':
listfindbyqualifications_experienceinmonthsgreaterthanandqualifications_experienceinmonthslessthanandname(int experiencegreater, int experienceless, string name);
或者你可以尝试使用这个相当不错的方法:
or you can try to use this pretty nice method:
@query("select p from person p left join p.qualifications q where q.experienceinmonths > ?1 and q.experienceinmonths < ?2 and q.name = ?3") listfindbyqualification(int experiencegreater, int experienceless, string name);