[英]How to make JPA @query work with projection that has another projection in it
I'm trying to get projections works on @query
in JPA with joined columns as an another project as well.我正在尝试将 JPA 中的
@query
与连接列一起作为另一个项目进行投影。
Say, I have 2 entities: course and student and student can have many courses and the foreign key is on the course table:说,我有 2 个实体:课程和学生,学生可以有很多课程,外键在课程表上:
student(id, name, email, age, grade, uuid)
course(id, name, uuid, student_id)
And I have projections like these:我有这样的预测:
public interface CourseProjection {
UUID getUuid();
}
And,和,
public interface StudentProjection {
String getName();
String getEmail();
Set<CourseProjection> getCourses();
}
In the entity:在实体中:
public class Student {
// other code
@OneToMany(mappedBy = "student")
private Set<Course> courses = new LinkedHashSet<>();
}
I need to do LIKE
search on multiple columns including course uuid.我需要对包括课程 uuid 在内的多个列进行
LIKE
搜索。 I also need I also want pagination.我也需要我也想要分页。 So, in the repository, I want to do something like:
所以,在存储库中,我想做类似的事情:
@Repository
interface StudentRepository extends JpaRepository<Student, UUID> {
@Query(
value = "SELECT * FROM student s LEFT JOIN courses c ON s.id =
c.student_id WHERE s.name LIKE %str% OR c.student_id::text LIKE %str%",
nativeQuery = true
)
Page<StudentProjection> findAllByKeyword(String keyword, Pageable pageable);
}
I want the result looks like this:我希望结果如下所示:
[
{
name: John
email: johndoe@gmail.com
courses: [
"course_uuid_1",
"course_uuid_2"
]
}
{
...
}
]
Any suggestions on how to achieve this?关于如何实现这一目标的任何建议?
Do you have try:你有没有尝试:
interface StudentRepository extends JpaRepository, excerptProjection = projectionName.class To hard this projection to the repository. interface StudentRepository extends JpaRepository, excerptProjection = projectionName.class 硬将此投影到存储库。
Https:domain/contextpath/students/search/findAllByKeyword?projection=projectionName To apply projection for this rest api. https:domain/contextpath/students/search/findAllByKeyword?projection=projectionName 为这个 rest api 应用投影。 And you need add more configuration as below:
并且您需要添加更多配置,如下所示:
@Configuration public class RepositoryConfig extends RepositoryRestConfigurerAdapter { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.getProjectionConfiguration().addProjection(projectionName.class)); } }
When using Spring Data nested closed projections with custom queries , you need to make sure you are definitely:在将 Spring Data嵌套封闭投影与自定义查询一起使用时,您需要确保您确定:
I ran into the same issue, trying to figure out how to solve it.我遇到了同样的问题,试图弄清楚如何解决它。 Your code would've worked totally fine if you just had done:
如果您刚刚完成,您的代码将完全正常工作:
public interface StudentProjection {
String getName();
String getEmail();
Set<CourseProjection> getCourses();
interface CourseProjection {
UUID getUuid();
}
}
And then used your custom query like this:然后像这样使用您的自定义查询:
@Query(nativeQuery = true,
value = "SELECT s.name as name, s.email as email, c.uuid as uuid "
+ "FROM student s "
+ "LEFT JOIN courses c ON s.id = c.student_id "
+ "WHERE s.name LIKE %str% OR c.student_id::text LIKE %str%")
Page<StudentProjection> findAllByKeyword(String keyword, Pageable pageable);
I also believe there was no need for using a native query, you could've just used HQL/JPQL
instead or even Spring Data query methods
.我还相信没有必要使用本机查询,您可以只使用
HQL/JPQL
甚至 Spring Data query methods
。
I know this question is a bit old, but it can be helpful to other people out there.我知道这个问题有点老了,但它对外面的其他人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.