使用Spring JPA访问多个实体连接查询结果集

时间:2016-04-06 21:24:54

标签: java entity spring-data-jpa

我有2个实体类1.student 2. resluts,我必须通过执行以下自定义查询返回结果集

 select s.roll_no , s.first_name, s.age ,r.subject_name , r.marks from student s , results r where s.roll_no= : rollNo and r.marks >70

给出了结果集,包括学生和结果实体。在这种情况下,我如何编写我的实现。我试过以下两种方法

方法1:

public interface GetStudentDetail extends CrudRepository<Student, String> {
    @Transactional(readOnly=true)
    @Query("select s.roll_no , s.first_name, s.age ,r.subject_name , r.marks from student s , results r where s.roll_no= : rollNo and r.marks >70")
    public List<student> getStudentDetails(@Param("rollNo")String rollNo);
}

通过上述内容,我只能从结果集中获取学生实体值,并且结果实体对象在此处不​​可见。

方法2:

public interface GetStudentDetail extends CrudRepository<Student, String> {
    @Transactional(readOnly=true)
    @Query("select s.roll_no , s.first_name, s.age ,r.subject_name , r.marks from student s , results r where s.roll_no= : rollNo and r.marks >70")
    public List<Object[]> getStudentDetails(@Param("rollNo")String rollNo);
}

通过这个,我创建了一个包含实体变量的VO类,并通过访问它的位置手动设置为这些对象,如下所示

List<StudentResultVo>studtVoObjList = new ArrayList<StudentResultVo>();
    for (Object[] resObj : resultSetList) {
        StudentResultVo studtVO = new StudentResultVo();
        if (resObj[0] != null) {
            studtVO.setRollNo(resObj[0].toString().trim());
        }
        //.First name
        //.Age

        if (resObj[3] != null) {
            studtVO.setSubName(resObj[3].toString().trim());
        }
        //.Marks
        studtVoObjList.add(studtVO);
    }

我知道上述方法不是一个好的编码实践。有没有办法解决这个问题。

感谢。

1 个答案:

答案 0 :(得分:2)

首先创建新界面并声明方法

public class LaunchVehicle {


    private String name;
    private String country;
    private Date firstFlight;
    private Map<String, Float> characteristics;
    private SpaceAgency spaceAgency;

    @JsonCreator
    constructor...

    @JsonProperty(required=false)
    getters and setters...

}

和第二个创建类public interface GetStudentDetailCustom { public List<Object[]> getStudentDetails(String rollNo); } 并实现接口

GetStudentDetailImpl

并像这样重构您的存储库

public class GetStudentDetailImpl implements GetStudentDetailCustom {

    @PersistenceContext
    private EntityManager entitymanager; // I use entity manager also you want to use inject SessionFactory etc..

    @Override
    public List<Object[]> getStudentDetails(String rollNo) {
        String queryString = "SELECT s.roll_no , s.first_name, s.age ,r.subject_name , r.marks FROM student s "
                + ", results r WHERE s.roll_no= :rollNo and r.marks > 70";

        Query query = entitymanager.createQuery(queryString);
        query.setParameter("rollNo", rollNo);

        return query.getResultList();
    }

}

最后在您的服务类中使用注入public interface GetStudentDetail extends CrudRepository<Student, String>, GetStudentDetailCustom {} 并调用GetStudentDetail方法

在服务层中使用示例:

getStudentDetails

参考答案:How to add custom method to Spring Data JPA

和春季参考:http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour