Spring Boot Webflux R2dbc一对一关系表问题

rsl1atfo  于 2024-01-06  发布在  Spring
关注(0)|答案(4)|浏览(303)

大家好,我正在用R2 dbc(非阻塞)编写Rest API项目,但问题是表关系
任务模型

  1. @Getter
  2. @Setter
  3. @Entity(name = "task")
  4. @Table(name = "task")
  5. public class Task {
  6. @Id
  7. @Column(name = "id",unique = true,nullable = false)
  8. private Integer id;
  9. @Column(name = "comment_count")
  10. private Integer comment_count;
  11. @Column(name = "completed")
  12. private Boolean completed;
  13. @Column(name = "content")
  14. private String content;
  15. @Column(name = "url")
  16. private String url;
  17. @Column(name = "orderdata")
  18. private Integer orderdata;
  19. @Column(name = "priority")
  20. private Integer priority;
  21. @OneToOne
  22. @JoinColumn(name = "due_id")//this is relation table Due but this relation retunr response null
  23. private Due due;
  24. }

字符串

到期型号

  1. @Getter
  2. @Setter//Lombok library
  3. @Entity
  4. @Table(name = "due")
  5. public class Due {
  6. @Id
  7. @Column(nullable = false, unique = true, updatable = false)
  8. private Integer id;
  9. @Column(name = "date_data")
  10. private Date date_Data;
  11. @Column
  12. private Boolean recurring;
  13. @Column(name = "string_data")
  14. private String string_data;
  15. }

Tasks Repository//此React式crud repositroy

  1. @Repository
  2. public interface TasksRepository extends ReactiveCrudRepository<Task,Integer> {
  3. }

任务管理器//这是业务层

  1. @Component
  2. @Slf4j
  3. @RequiredArgsConstructor
  4. public class TaskManager {
  5. private final TasksRepository tasksRepository;
  6. public Flux<Task> findAll() {
  7. return tasksRepository.findAll();
  8. }
  9. }

任务服务//此为服务层

  1. @Service
  2. @RequiredArgsConstructor
  3. public class TasksService {
  4. private final TaskManager taskManager;
  5. public Flux<Task> getAll() {
  6. return taskManager.findAll();
  7. }
  8. }

任务控制器

  1. @RestController
  2. @RequestMapping("/api/tasks")
  3. @RequiredArgsConstructor
  4. public class TasksController {
  5. private final TasksService tasksService;
  6. @GetMapping
  7. public Flux<Task> getAllTasks(){
  8. return tasksService.getAll();
  9. }
  10. }


任务响应
我的Rest API响应任务

  1. {
  2. "id": 1,
  3. "comment_count": 10,
  4. "completed": false,
  5. "content": "new content",
  6. "url": "http:8085.com",
  7. "orderdata": 5,
  8. "priority": 5,
  9. "due": {
  10. "id": 3,
  11. "date_Data": null,
  12. "recurring": null,
  13. "string_data": null
  14. }

问题一对一关系响应空什么是问题?请帮助我谢谢

误差轨迹

  1. 2021-03-20 20:49:57.161 ERROR 13652 --- [actor-tcp-nio-1]
  2. r.n.channel.ChannelOperationsHandler : [id: 0x1126662b,
  3. L:/127.0.0.1:55827 - R:localhost/127.0.0.1:5432] Error was received while
  4. reading the incoming data. The connection will be closed.
  5. java.lang.StackOverflowError: null
  6. at io.netty.util.internal.ReferenceCountUpdater
  7. .isLiveNonVolatile(ReferenceCountUpdt
  8. er.java:88) ~[netty-common-4.1.59.Final.jar:4.1.59.Final]
  9. at
  10. io.netty.buffer.AbstractReferenceCountedByteBuf
  11. .isAccessible(AbstractReferenceCountedByteBuf.java:56) ~[netty-buffer-
  12. 4.1.59.Final.jar:4.1.59.Final]
  13. at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1455) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  14. at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1385) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  15. at io.netty.buffer.UnsafeByteBufUtil.getBytes(UnsafeByteBufUtil.java:481) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  16. at io.netty.buffer.PooledUnsafeDirectByteBuf.getBytes(PooledUnsafeDirectByteBuf.java:130) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  17. at io.netty.buffer.PooledSlicedByteBuf.getBytes(PooledSlicedByteBuf.java:235) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  18. at io.netty.buffer.ByteBufUtil.decodeString(ByteBufUtil.java:1147) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  19. at io.netty.buffer.AbstractByteBuf.toString(AbstractByteBuf.java:1248) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  20. at io.netty.buffer.AbstractByteBuf.getCharSequence(AbstractByteBuf.java:515) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  21. at io.netty.buffer.AbstractByteBuf.readCharSequence(AbstractByteBuf.java:520) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
  22. at io.r2dbc.postgresql.util.ByteBufUtils.decode(ByteBufUtils.java:42) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  23. at io.r2dbc.postgresql.codec.AbstractNumericCodec.decodeNumber(AbstractNumericCodec.java:122) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  24. at io.r2dbc.postgresql.codec.AbstractNumericCodec.decodeNumber(AbstractNumericCodec.java:89) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  25. at io.r2dbc.postgresql.codec.IntegerCodec.doDecode(IntegerCodec.java:49) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  26. at io.r2dbc.postgresql.codec.IntegerCodec.doDecode(IntegerCodec.java:30) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  27. at io.r2dbc.postgresql.codec.AbstractCodec.decode(AbstractCodec.java:82) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  28. at io.r2dbc.postgresql.codec.DefaultCodecs.decode(DefaultCodecs.java:149) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  29. at io.r2dbc.postgresql.PostgresqlRow.decode(PostgresqlRow.java:90) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  30. at io.r2dbc.postgresql.PostgresqlRow.get(PostgresqlRow.java:77) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
  31. at io.r2dbc.spi.Row.get(Row.java:76) ~[r2dbc-spi-0.8.3.RELEASE.jar:na]
  32. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:173) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  33. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  34. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  35. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  36. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  37. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  38. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  39. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  40. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  41. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  42. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  43. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  44. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  45. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  46. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  47. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
  48. at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]

fafcakar

fafcakar1#

R2DBC不支持关系,因此JoinColumn注解将不起作用。相反,您可以创建一个DueRepository并手动获取此实体。

bsxbgnwa

bsxbgnwa2#

当我在一个服务中做一些查询时,我遇到了这个情况。考虑到“R2DBC”不支持“JoinColumn”,我做的是在仓库中构建一个自定义查询。

  1. @Query("SELECT * FROM task INNER JOIN due USING (id) WHERE id=:id")
  2. Mono<NewDto> getCompleteTrx(String id);

字符串
但重要的是,您有一个Dto,其中包含要连接的两个表的对象,但它没有“table”注解。

  1. @Getter
  2. @Setter
  3. @Entity
  4. public class NewDto { // depends on the relation you need
  5. @Column(name = "id")
  6. private Integer id;
  7. @Column(name = "comment_count")
  8. private Integer comment_count;
  9. @Column(name = "completed")
  10. private Boolean completed;
  11. @Column(name = "content")
  12. private String content;
  13. @Column(name = "url")
  14. private String url;
  15. @Column(name = "orderdata")
  16. private Integer orderdata;
  17. @Column(name = "priority")
  18. private Integer priority;
  19. @Column(name = "date_data")
  20. private Date date_Data;
  21. @Column(name = "string_data")
  22. private String string_data;
  23. ...
  24. }


通过这样做,我可以解决我的问题,因为我需要连接3个表,我不想做3个查询,这样只需要一个查询。然后你可以将DTOMap到你需要的对象。

展开查看全部
bejyjqdl

bejyjqdl3#

我认为您还应该将源代码中的层数限制为Controller和Service(业务逻辑在这里)。

wb1gzix0

wb1gzix04#

好吧,伙计,首先,为什么这个“到期”实体需要成为它自己的实体?我的意思是,一个任务可能有一个截止日期,它可能是重复的。保持简单。
关于代码,不考虑域建模方面:
使用Long或UUID作为ID字段类型。就我个人而言,我从未在生产环境中见过int。
为这些“到期”实体创建一个CrudRepo,检查是否可以自己查询它们。编写一个测试来验证这一点。
我的猜测是“JoinTable”注解是多余的和/或配置错误的。尝试“Due.ID”而不是“Due_ID”来告诉它应该接受Due对象的哪个字段。

相关问题