有人可以指导我如何在 jOOQ 中使用别名吗?我尝试查看 jOOQ 文档,但不清楚。如果可能,请提供示例。
我正在编写一个网络服务。我想知道DSLContext创建对象是否很重?我可以在 DAO 方法中创建它们(并在方法完成时销毁它们)吗?或者最好在请求开始时创建一个并将其传递给 DAO 类。
以下 SQL 有效
CREATE TABLE stored_file (
id INT AUTO_INCREMENT NOT NULL,
content BLOB,
content_length LONG,
PRIMARY KEY (id)
);
UPDATE stored_file SET content_length = length(content)
Run Code Online (Sandbox Code Playgroud)
但我不能在 jOOQ 中做同样的事情。
getContext().update(STORED_FILE)
.set(STORED_FILE.CONTENT_LENGTH, DSL.length(STORED_FILE.CONTENT))
Run Code Online (Sandbox Code Playgroud)
DSL.length 仅允许 String 字段类型。
有没有解决的办法?
请建议JOOQ DSL来查询布尔表达式的结果
在SQL中我会写:
SELECT sum(apples.quantity) > sum(bananas.quantity)
FROM ...
Run Code Online (Sandbox Code Playgroud) 我在 Java 中收到 org.jooq.exception.DataAccessException 和一条消息
错误:由于事务之间的读/写依赖性,无法序列化访问详细信息:原因代码:在检查冲突期间,在识别为枢轴时被取消。提示:如果重试,事务可能会成功。
我想检查数据访问异常的错误代码,这是幕后的Postgres数据库。例如,序列化失败异常错误代码为40001。
如何检查 org.jooq.exception.DataAccessException 的错误代码?
我的动机是如果序列化失败则重试事务。
我正在尝试组装一个 SQL 生成器,以便我可以动态构建 SQL 语句,并将它们传递到 Spark 作业服务器。当表列预先知道时,这似乎很容易(使用 JOOQ):
String sql = DSL.select(field("field-1"), field("field-2"), field("field-3"))
.from(table("myTable"))
Run Code Online (Sandbox Code Playgroud)
然而,这里的目标是能够解析传入的 HTTP POST,从中获取 JSON 模式,并根据 JSON 中描述的表结构生成 SQL 选择语句。
...
List<String> fieldNames = new ArrayList<>();
fieldNames.add("field-1");
fieldNames.add("field-2");
fieldNames.add("field-3");
Run Code Online (Sandbox Code Playgroud)
...
JOOQ“字段”似乎是一个强类型对象。
有没有办法使用这种动态构造的列列表来构建这样的 JOOQ select 语句?
我使用 spring boot 和 JOOQ 创建了一个简单的项目,添加了依赖项“spring-boot-starter-jooq”。当我编译时,应用程序无法启动。
这是 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bingo</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>api</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hashids</groupId>
<artifactId>hashids</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> …Run Code Online (Sandbox Code Playgroud) 我有代码从 2 个表中检索 2 个对象(由 JooQ 生成的 POJO)的一条记录。
Record record = dsl.select()
.from(ISSUE)
.leftJoin(CLIENT).on(CLIENT.ID.eq(ISSUE.CLIENT_ID))
.where(ISSUE.ID.eq(id))
.fetchOne();
JIssue jIssue = record.into(JIssue.class);
JClientRecord jClient = record.into(JClientRecord.class);
Run Code Online (Sandbox Code Playgroud)
表 ISSUE 和表 CLIENT 都有 PK 字段“ID”。
问题是当映射到(POJO.class)时,具有相同名称的表字段未正确映射到 POJO。在上面的示例中,jIssue 采用 jClient 的 id。
当使用 TableRecords 而不是 POJO 的所有映射正确完成时,但您不能在生成的 DAO 中使用 TableRecord,它需要生成的 POJO。
我该如何解决这个问题,以便 jooq 可以正确映射到生成的 pojo 字段?
我很想知道在使用OFFSET和LIMIT
我们尝试使用 JOOQs 合成 SEEK 子句,但是因为我们的 ID 是无序的 UUID,所以它不起作用。
我们当前的实现是执行查询两次,第一次是在我们设置偏移量和限制以获取结果/行数之前。
然后我们在第二个查询中得到结果。
SelectQuery<Record> select = context.selectQuery();
select.addSelect(FOO_TABLE.fields());
select.addFrom(FOO_TABLE);
int totalElements = select.fetch().size();
select.addOffset(20);
select.addLimit(50));
List<Foo> paginatedFoo = select.fetchInto(Foo.class);
Run Code Online (Sandbox Code Playgroud)
这只是这个实现/设计必须接受的东西,还是有任何 JDBC 驱动程序魔术可以消除在数据库上执行两个查询的需要?
欢迎任何意见或建议!
我刚刚尝试将我的项目升级到 Java 15,现在出现以下错误:
both interface org.jooq.Record in org.jooq and class java.lang.Record in java.lang match
Run Code Online (Sandbox Code Playgroud)
有没有人有解决这个问题的经验?
java ×10
jooq ×10
sql ×3
alias ×1
exception ×1
java-15 ×1
java-record ×1
jdbc ×1
mapping ×1
pojo ×1
postgresql ×1
spring ×1
spring-boot ×1