简体   繁体   English

Jooq - 将表和 map 连接到另一个 pojo

[英]Jooq - Join tables and map to another pojo

I have this classes:我有这个课程:

class Student {
    String student_id;
    String name;
    List<GradeValue> grades;
}


class Grades {
    String id;
    String student_id;
    Long grade_value;
}


class GradeValue {
    Long grade_value;
}

I'm trying to use JOOQ to make a query that returns all students with all the grade values.我正在尝试使用 JOOQ 进行查询,以返回具有所有成绩值的所有学生。

For this to happen I have to join the student and grades tables but then I should convert the grade to a grade value and I'm not sure how to do this.为此,我必须加入学生和成绩表,但我应该将成绩转换为成绩值,但我不知道该怎么做。

List<Student> students = dsl.select(jsonObject(
    jsonEntry("id", STUDENT.STUDENT_ID),
    jsonEntry("name", STUDENT.NAME),
    jsonEntry("grades", field(
        select(jsonbObjectAgg(GRADES.GRADE_VALUE))
        .from(GRADES)
        .where(GRADES.STUDENT_ID.eq(STUDENT.STUDENT_ID))
    ))
)).from(STUDENT)
.fetchInto(Student.class);

Gives me this error:给我这个错误:

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<GradeValue> out of START_OBJECT token at [Source: (String)"{"id": "1", "name": "test", "grades": {"grade_value": 10}}"; Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<GradeValue> out of START_OBJECT token at [Source: (String)"{"id": "1", "name “:“测试”,“成绩”:{“grade_value”:10}}”; line: 1, column: 99]行:1,列:99]

Any ideias?有什么想法吗?

Thanks谢谢

There are numerous problems with your code snippet:您的代码段存在许多问题:

Using jsonArrayAgg() instead of jsonbObjectAgg()使用jsonArrayAgg()而不是jsonbObjectAgg()

You didn't want to aggregate your grades into an object, I think, but into an array?我认为,您不想将成绩汇总到 object 中,而是汇总到数组中? Use jsonArrayAgg() , instead.请改用jsonArrayAgg() See these manual sections for details:有关详细信息,请参阅以下手册部分:

Also, I suggest deciding for either using JSON or JSONB methods, not to mix them.另外,我建议决定使用JSONJSONB方法,不要混合使用它们。 But the way you wrote the query, that works for但是您编写查询的方式适用于

class Student {
    String student_id;
    String name;
    List<Long> grades;
}

Since you're wrapping the grade in another class, you have to reflect that as well in your generated JSON structure:由于您将等级包装在另一个 class 中,因此您还必须在生成的 JSON 结构中反映这一点:

List<Student> students = dsl.select(jsonObject(
    key("student_id").value(STUDENT.STUDENT_ID),
    key("name").value(STUDENT.NAME),
    key("grades").value(field(
        // See fix here
        select(jsonArrayAgg(jsonObject(
            key("grade_value").value(GRADES.GRADE_VALUE)
        )))
        .from(GRADES)
        .where(GRADES.STUDENT_ID.eq(STUDENT.STUDENT_ID))
    ))
)).from(STUDENT)
.fetchInto(Student.class);

Fetching into STUDENT.class进入STUDENT.class

Perhaps just a typo, but you wrote:也许只是一个错字,但你写道:

.fetchInto(STUDENT.class)

You probably meant to write:你可能打算写:

.fetchInto(Student.class)

Projecting the right attribute names投影正确的属性名称

Your Student class has a student_id attribute, but you're projecting id :您的Student class 具有student_id属性,但您正在投影id

jsonEntry("id", STUDENT.STUDENT_ID)

Better rename that attribute to id also, or project "student_id"最好将该属性也重命名为id ,或项目"student_id"

Aggregating empty sets聚合空集

Note that JSON_ARRAYAGG() aggregates empty sets into NULL , not into an empty [] .请注意, JSON_ARRAYAGG()将空集聚合到NULL中,而不是空[]中。 If that's a problem, use COALESCE()如果这是一个问题,请使用COALESCE()

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

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