如何使用@SqlResultSetMapping将ONE-TO-MANY本机查询结果映射到POJO类中

时间:2017-12-24 08:37:20

标签: one-to-many jpa-2.1 nativequery sqlresultsetmapping

我使用Java和MySql在后端API中工作,我正在尝试在JPA 2.1中使用@SqlResultSetMapping将ONE-TO-MANY本机查询结果映射到POJO类,这是本机查询:

@NamedNativeQuery(name = "User.getAll”, query = "SELECT DISTINCT t1.ID, t1.RELIGION_ID t1.gender,t1.NAME,t1.CITY_ID , t2.question_id, t2.answer_id  FROM user_table t1 inner join user_answer_table t2 on t1.ID = t2.User_ID“,resultSetMapping="userMapping")

而且,这是我的结果SQL映射:

@SqlResultSetMapping(
            name = "userMapping",
            classes = {
                            @ConstructorResult(
                                    targetClass = MiniUser.class,
                                    columns = {
                                            @ColumnResult(name = "id"),
                                            @ColumnResult(name = "religion_id"),
                                            @ColumnResult(name = "gender"),
                                            @ColumnResult(name = "answers"),
                                            @ColumnResult(name = "name"),
                                            @ColumnResult(name = "city_id")

                                    }
                            ),

                            @ConstructorResult(
                                    targetClass = MiniUserAnswer.class,
                                    columns = {
                                            @ColumnResult(name = "question_id"),
                                            @ColumnResult(name = "answer_id")

                                    }
                              )

            })

而且,这是POJO类的实现:(我刚刚删除了构造函数和getter / setter)

MiniUser类

public class MiniUser {

    String id;
    String religionId;
    Gender gender;
    List<MiniUserAnswer> answers;
    String name;
    String city_id;
}

和MiniUserAnswer类

public class MiniUserAnswer {

    String questionId;
    String answerId;
}

我的目标是执行此查询并返回MiniUser的列表,并在每个MiniUser中列出他的“答案”列表,这是MiniUserAnswer的列表。

运行此代码后,我收到此错误:

The column result [answers] was not found in the results of the query.

我知道原因,这是因为查询选择语句中没有“答案”字段 那么,考虑到性能,我怎样才能完成这样的事情呢?此答案列表可能达到100。

非常感谢您的帮助,提前致谢!

1 个答案:

答案 0 :(得分:0)

查询“ SELECT DISTINCT t1.ID,t1.RELIGION_ID,t1.gender,t1.NAME,t1.CITY_ID,t2.question_id,t2.answer_id”不会返回称为答案的参数。 要获得您要寻找的结果,我将使用:

选项1(条件生成器)

jQuery('div').on('scroll', function(event){
var top = jQuery('#scroll_container').scrollTop();
var height = jQuery('#scroll_container').scrollHeight;
var width = top/height * 100;
jQuery('#progess-bar').css('width', width+"%");
});

选项2(命名查询,非本地查询)

@NamedQuery(name =“ User.getAll”,query =“ SELECT t1 from UserTableEntityt1 join fetch t1.answers)

选项3(实体子图,JPA 2.1中的新功能)

在“用户实体”类中:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<UserTableEntity> cq = cb.createQuery(UserTableEntity.class);
Root<UserTableEntity> rootUserTable = cq.from(UserTableEntity.class);
Join<UserTableEntity,UserAnswerTableEntity> joinAnswerTable = rootUserTable.join(rootUserTable_.id) // if the relationship is defined as lazy, use "fetch" instead of "join"
//cq.where() NO WHERE CLAUSE
cq.select(rootUserTable)

entityManager.createQuery(cq).getResultList();

在DAO中,在实体管理器中设置提示:

@NamedEntityGraphs({
    @NamedEntityGraph(name = "graph.User.Answers", attributeNodes = @NamedAttributeNode("answers"))
})