Spring Boot Jooq Dao麻烦
我是Java和Spring的新手,我正在做我的第一个休息服务。我遇到了一个无法找到答案的问题。
我使用jooq和pojos和daos生成了实体,
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.16.6</version>
<executions>
...
</executions>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/spring</url>
<user>postgres</user>
<password>${env.PGPASSWORD}</password>
</jdbc>
<generator>
<name>org.jooq.codegen.JavaGenerator</name>
<database>
...
</database>
<target>
<packageName>com.example.otrtesttask.jooq</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
<generate>
<pojos>true</pojos>
<daos>true</daos>
<records>true</records>
</generate>
</generator>
</configuration>
</plugin>
我的数据库结构:
CREATE TABLE branch(id SERIAL PRIMARY KEY, title VARCHAR(100));
CREATE TABLE position(id SERIAL PRIMARY KEY, title VARCHAR(30));
CREATE TABLE employee
(
id SERIAL PRIMARY KEY,
manager_id INTEGER REFERENCES employee (id),
position_id INTEGER REFERENCES position (id) NOT NULL,
full_name VARCHAR(50) NOT NULL,
branch_id INTEGER REFERENCES branch (id) NOT NULL);
CREATE TABLE task
(
id SERIAL PRIMARY KEY,
priority SMALLINT NOT NULL,
description VARCHAR(200) NOT NULL,
employee_id INT REFERENCES employee (id) NOT NULL);
然后我制作了调用服务的控制器,该控制器调用存储库来调用Crud操作。 而且它的工作原理非常好,但是我想获得位置标题以及ID等。所以我做了一个DTO:
@Data
public class EmployeeDto {
private Integer id;
private Integer managerId;
private Integer positionId;
private String fullName;
private Integer branchId;
private Employee manager;
private Position position;
private Branch branch;
}
之后,我做了一个将员工转换为Emplyeedto的映射器。有人告诉我,获取嵌套数据的最佳方法(例如位置标题)是使用dao fetchonybyId函数。
@Service
public class MappingUtils {
EmployeeDao employeeDao = new EmployeeDao();
PositionDao positionDao = new PositionDao();
BranchDao branchDao = new BranchDao();
public EmployeeDto mapToEmployeeDto(Employee employee) {
EmployeeDto dto = new EmployeeDto();
dto.setId(employee.getId());
dto.setBranchId(employee.getBranchId());
dto.setPositionId(employee.getPositionId());
dto.setFullName(employee.getFullName());
dto.setManagerId(employee.getManagerId());
dto.setManager(employeeDao.fetchOneById(employee.getManagerId()));
dto.setPosition(positionDao.fetchOneById(employee.getPositionId()));
dto.setBranch(branchDao.fetchOneById(employee.getBranchId()));
return dto;
}
我绘制了这样的存储库中的实体:
@Repository
@RequiredArgsConstructor
public class EmployeeRepository {
@Autowired
private final DSLContext dsl;
private final MappingUtils mappingUtils;
public List<EmployeeDto> findAll(Condition condition) {
return dsl.selectFrom(Tables.EMPLOYEE)
.where(condition)
.fetchInto(Employee.class)
.stream().map(mappingUtils::mapToEmployeeDto)
.collect(Collectors.toList());
}
}
它可以正常运行,直到达到DAO提取功能为止。它引发了异常org.jooq.exception.detachedexception:无法执行查询。未配置JDBC连接
。 删除DAO函数后,GET查询返回的响应不错,位置和分支设置为null。
我在这里做错了什么?以及如何提供必要的连接?
== upd == 我的应用程序。
spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
spring.datasource.password=${PGPASSWORD}
spring.liquibase.change-log=classpath:liquibase/changelog.sql
I'm new to Java and Spring and I am doing my first rest service. I've encountered a problem to which I cannot find an answer.
I generated entities using Jooq with pojos and daos
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.16.6</version>
<executions>
...
</executions>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/spring</url>
<user>postgres</user>
<password>${env.PGPASSWORD}</password>
</jdbc>
<generator>
<name>org.jooq.codegen.JavaGenerator</name>
<database>
...
</database>
<target>
<packageName>com.example.otrtesttask.jooq</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
<generate>
<pojos>true</pojos>
<daos>true</daos>
<records>true</records>
</generate>
</generator>
</configuration>
</plugin>
My database structure:
CREATE TABLE branch(id SERIAL PRIMARY KEY, title VARCHAR(100));
CREATE TABLE position(id SERIAL PRIMARY KEY, title VARCHAR(30));
CREATE TABLE employee
(
id SERIAL PRIMARY KEY,
manager_id INTEGER REFERENCES employee (id),
position_id INTEGER REFERENCES position (id) NOT NULL,
full_name VARCHAR(50) NOT NULL,
branch_id INTEGER REFERENCES branch (id) NOT NULL);
CREATE TABLE task
(
id SERIAL PRIMARY KEY,
priority SMALLINT NOT NULL,
description VARCHAR(200) NOT NULL,
employee_id INT REFERENCES employee (id) NOT NULL);
Then I made Controller that calls Service that calls Repository to invoke CRUD operations.
And it works perfectly fine, but I want to get position title along with id, etc. So I made a DTO:
@Data
public class EmployeeDto {
private Integer id;
private Integer managerId;
private Integer positionId;
private String fullName;
private Integer branchId;
private Employee manager;
private Position position;
private Branch branch;
}
After that I made a mapper that converts Employee to EmplyeeDto. I've been told that the best way to get nested data (like position title) is to use DAO fetchOnyById function.
@Service
public class MappingUtils {
EmployeeDao employeeDao = new EmployeeDao();
PositionDao positionDao = new PositionDao();
BranchDao branchDao = new BranchDao();
public EmployeeDto mapToEmployeeDto(Employee employee) {
EmployeeDto dto = new EmployeeDto();
dto.setId(employee.getId());
dto.setBranchId(employee.getBranchId());
dto.setPositionId(employee.getPositionId());
dto.setFullName(employee.getFullName());
dto.setManagerId(employee.getManagerId());
dto.setManager(employeeDao.fetchOneById(employee.getManagerId()));
dto.setPosition(positionDao.fetchOneById(employee.getPositionId()));
dto.setBranch(branchDao.fetchOneById(employee.getBranchId()));
return dto;
}
And I map entities inside Repository like this:
@Repository
@RequiredArgsConstructor
public class EmployeeRepository {
@Autowired
private final DSLContext dsl;
private final MappingUtils mappingUtils;
public List<EmployeeDto> findAll(Condition condition) {
return dsl.selectFrom(Tables.EMPLOYEE)
.where(condition)
.fetchInto(Employee.class)
.stream().map(mappingUtils::mapToEmployeeDto)
.collect(Collectors.toList());
}
}
And it works fine until it gets to the dao fetch function. It throws an exception org.jooq.exception.DetachedException: Cannot execute query. No JDBC Connection configured
.
Once I remove the dao functions the get query returns nice response with manager, position and branch set to null.
What am I doing wrong here? And how to provide necessary connection?
==UPD==
My application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
spring.datasource.password=${PGPASSWORD}
spring.liquibase.change-log=classpath:liquibase/changelog.sql
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的DAO类未连接到JOOQ
配置
。您只是这样创建了它们:但是您必须这样创建它们:
您也可以配置代码生成器以生成弹簧注释,然后将daos注入您的类。配置将是:
然后:
The problem is that your DAO classes aren't attached to a jOOQ
Configuration
. You just created them like this:But you have to create them like this:
You can also configure the code generator to generate Spring annotations and then inject the DAOs to your class. The configuration would be:
And then: