8

我在使用 Java 和 MySql 的后端 API 中工作,我正在尝试使用 JPA 2.1 中的 @SqlResultSetMapping 将一对多本机查询结果映射到 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。

非常感谢您的帮助,在此先感谢!

4

1 回答 1

0

查询“SELECT DISTINCT t1.ID, t1.RELIGION_ID t1.gender, t1.NAME, t1.CITY_ID, t2.question_id, t2.answer_id”不返回名为 answers 的参数。为了获得您正在寻找的结果,我将使用:

选项 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();

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

@NamedQuery(name = "User.getAll", query = "SELECT t1 FROM UserTableEntityt1 join fetch t1.answers)

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

在用户实体类中:

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

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

EntityGraph graph = this.em.getEntityGraph("graph.User.Answers");

Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);
于 2019-02-28T07:34:01.717 回答