Java 如何在使用JOOQ进行内部连接时正确使用WHERE?

Java 如何在使用JOOQ进行内部连接时正确使用WHERE?,java,spring,jooq,Java,Spring,Jooq,我刚刚在一个Postgres数据库上为JOOQ建立了一个Spring Boot项目,我很喜欢它!我不认为在这之后我会使用ORMs,但我真的有问题,没有这么简单的CRUD查询 此时,我不知道我的JOOQ查询是否写得很糟糕,或者是否没有按预期处理返回类型 我基本上是想在两个表之间建立一个简单的内部联接: - 对于这两个表,我试图转换这个SQL查询 SELECT * FROM questions q INNER JOIN tests_content t ON q.question_id = t.que

我刚刚在一个Postgres数据库上为JOOQ建立了一个Spring Boot项目,我很喜欢它!我不认为在这之后我会使用ORMs,但我真的有问题,没有这么简单的CRUD查询

此时,我不知道我的JOOQ查询是否写得很糟糕,或者是否没有按预期处理返回类型

我基本上是想在两个表之间建立一个简单的内部联接:

-

对于这两个表,我试图转换这个SQL查询

SELECT *
FROM questions q
INNER JOIN tests_content t ON q.question_id = t.question_id 
WHERE t.test_id = 1007;
…转换为JOOQ查询。我尝试了一些方法,包括文档中的相关方法和我在这里找到的一些方法。基本上,我的JOOQ查询如下所示:

public Result<Record> queryMethod(){
    return dslContext.select().
        from(questions).
        join(testsContent).
        using(testsContent.QUESTION_ID).
        where(testsContent.QUESTION_ID.eq(1007)).fetch();
}

编辑2:添加查询的记录器代码:

DEBUG 2786 --- [nio-8080-exec-1] org.jooq.tools.LoggerListener:
Executing query: 
    select "public"."questions"."question_id", "public"."questions"."question_body", "public"."questions"."question_explanation", "public"."questions"."question_answer", "public"."tests_content"."question_id", "public"."tests_content"."test_id" 
    from "public"."questions"
    join "public"."tests_content" 
    using ("question_id")
    where "public"."tests_content"."question_id" = ?
DEBUG 2786 --- [nio-8080-exec-1] org.jooq.tools.LoggerListener: 
-> with bind values: 
    select "public"."questions"."question_id", "public"."questions"."question_body", "public"."questions"."question_explanation", "public"."questions"."question_answer", "public"."tests_content"."question_id", "public"."tests_content"."test_id" 
    from "public"."questions" 
    join "public"."tests_content"
    using ("question_id") 
    where "public"."tests_content"."question_id" = 1007
DEBUG 2786 --- [nio-8080-exec-1] org.jooq.tools.LoggerListener: Fetched result: 
+-----------+-------------+--------------------+---------------+-----------+-------+
|question_id|question_body|question_explanation|question_answer|question_id|test_id|
+-----------+-------------+--------------------+---------------+-----------+-------+
Fetched row(s)           : 0
2020-08-27 13:27:11.617  WARN 2786 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved 
[org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "favicon.ico"]

编辑3:

问题似乎出在WHERE条款上。我从JOOQ代码中删除了它,现在查询工作了

return dslContext.select(
                testsContent.QUESTION_ID,
                testsContent.TEST_ID,
                questions.QUESTION_BODY,
                questions.QUESTION_EXPLANATION,
                questions.QUESTION_ANSWER
).from(questions).join(testsContent).using(testsContent.QUESTION_ID).fetchInto(JoinQuestionsTest.class);
我还调整了POJO,因为JSON数据似乎是作为字符串处理的,而不是使用包含的JSON类型(我不明白后者存在的原因)。POJO现在看起来像这样:

@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class JoinQuestionsTest {
    private int question_id;
    private int test_id;
    private String question_body;
    private String question_explanation;
    private String question_answer;
}

目前正在试验/研究如何使.where()方法工作。

您好!“但我不能让它工作!”是什么意思?您是否看到编译错误、SQL错误或正在执行的SQL错误,或者完全不同的情况?“查询返回一个空数组”-好的,就是这样。现在我们需要看看生成的查询。Jooq在调试级别自动记录每个查询,因此启用调试日志以查看生成的内容,或者只需通过“getSQL()”调用将查询写入控制台。这个问题看起来像你期望的吗?@PetrJaneček嗨,谢谢你的回答。在通过请求(SpringBootRESTAPI)进行查询时,我得到了一个空数组作为响应。我知道JOOQ和API工作正常,因为首先我尝试了简单的查询,但现在当尝试复杂的查询时,作为简单的内部联接,我得到了一个空数组;但你在下面的测试表明你在查询问题Id eq(1007)。你确定你在正确的字段中使用了WHERE子句吗?@robvans你说得对!多大的错误啊!修复POJO并将JOOQ查询调整为
。其中(testsContent.TEST\u ID.eq(1007))
修复了该问题!谢谢你阅读我的问题!你刚刚救了我几个小时的调试。如果你愿意,把这个作为答案发表,我会标记它。干杯你好“但我不能让它工作!”是什么意思?您是否看到编译错误、SQL错误或正在执行的SQL错误,或者完全不同的情况?“查询返回一个空数组”-好的,就是这样。现在我们需要看看生成的查询。Jooq在调试级别自动记录每个查询,因此启用调试日志以查看生成的内容,或者只需通过“getSQL()”调用将查询写入控制台。这个问题看起来像你期望的吗?@PetrJaneček嗨,谢谢你的回答。在通过请求(SpringBootRESTAPI)进行查询时,我得到了一个空数组作为响应。我知道JOOQ和API工作正常,因为首先我尝试了简单的查询,但现在当尝试复杂的查询时,作为简单的内部联接,我得到了一个空数组;但你在下面的测试表明你在查询问题Id eq(1007)。你确定你在正确的字段中使用了WHERE子句吗?@robvans你说得对!多大的错误啊!修复POJO并将JOOQ查询调整为
。其中(testsContent.TEST\u ID.eq(1007))
修复了该问题!谢谢你阅读我的问题!你刚刚救了我几个小时的调试。如果你愿意,把这个作为答案发表,我会标记它。干杯
import org.jooq.JSON;

@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class JoinQuestionsTest {
    private int question_id;
    private int test_id;
    private String question_body;
    private JSON question_answer;
}
DEBUG 2786 --- [nio-8080-exec-1] org.jooq.tools.LoggerListener:
Executing query: 
    select "public"."questions"."question_id", "public"."questions"."question_body", "public"."questions"."question_explanation", "public"."questions"."question_answer", "public"."tests_content"."question_id", "public"."tests_content"."test_id" 
    from "public"."questions"
    join "public"."tests_content" 
    using ("question_id")
    where "public"."tests_content"."question_id" = ?
DEBUG 2786 --- [nio-8080-exec-1] org.jooq.tools.LoggerListener: 
-> with bind values: 
    select "public"."questions"."question_id", "public"."questions"."question_body", "public"."questions"."question_explanation", "public"."questions"."question_answer", "public"."tests_content"."question_id", "public"."tests_content"."test_id" 
    from "public"."questions" 
    join "public"."tests_content"
    using ("question_id") 
    where "public"."tests_content"."question_id" = 1007
DEBUG 2786 --- [nio-8080-exec-1] org.jooq.tools.LoggerListener: Fetched result: 
+-----------+-------------+--------------------+---------------+-----------+-------+
|question_id|question_body|question_explanation|question_answer|question_id|test_id|
+-----------+-------------+--------------------+---------------+-----------+-------+
Fetched row(s)           : 0
2020-08-27 13:27:11.617  WARN 2786 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved 
[org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "favicon.ico"]
return dslContext.select(
                testsContent.QUESTION_ID,
                testsContent.TEST_ID,
                questions.QUESTION_BODY,
                questions.QUESTION_EXPLANATION,
                questions.QUESTION_ANSWER
).from(questions).join(testsContent).using(testsContent.QUESTION_ID).fetchInto(JoinQuestionsTest.class);
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class JoinQuestionsTest {
    private int question_id;
    private int test_id;
    private String question_body;
    private String question_explanation;
    private String question_answer;
}