简体   繁体   English

使用自定义查询访问Spring Repository中的集合字段

[英]Access collection field in Spring Repository with custom Query

I'm trying to make a query to search all Posts from my application containing a keyword on the content or on the tags name but only those who are public. 我正在尝试进行查询,以搜索我的应用程序中的所有帖子,这些帖子在内容或标签名称上包含关键字,但仅包含那些公开的关键字。 I'm trying to make the JPQL search but I don't know how to access the tags.name property. 我正在尝试进行JPQL搜索,但是我不知道如何访问tags.name属性。

Note: Post is an entity which has a List of Tag entity; 注意: Post是具有Tag列表实体的实体;

I have tried this but it's not working (as I expected): 我已经尝试过了,但是没有用(正如我预期的那样):

@Query("SELECT p FROM Post p WHERE (p.content LIKE CONCAT('%', LOWER(:keyword),'%' OR p.tags.name LIKE CONCAT('%', LOWER(:keyword)) AND (p.open IS TRUE)")

I have looked at the documentation but I don't see any option to manage this, what is the best way to go here? 我已经看过文档,但是看不到任何管理方法,什么是最好的选择?

Thanks! 谢谢!

Since multiple tag s can be associated with a post the relationship is a @OneToMany from Post to Tag . 由于可以将多个tag与一个post相关联, @OneToManyPostTag的关系是@OneToMany A join should work in this scenario. 在这种情况下, join应该起作用。

Try this. 尝试这个。

@Query("SELECT p FROM Post p left join p.tags pTags WHERE (p.content LIKE CONCAT('%',LOWER(:keyword),'%' OR pTags.name LIKE CONCAT('%',LOWER(:keyword)) AND (p.open IS TRUE)")

PS : I have not tested this, but it should work. PS :我还没有测试过,但是应该可以。

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

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