Webflux R2dbc问题一对一关系表

Webflux R2dbc problem one-to-one relation tables

大家好,我正在使用 R2dbc(非阻塞)编写 Rest api 项目,但问题是 table 关系

任务模型

 @Getter
 @Setter
 @Entity(name = "task")
 @Table(name = "task")
 public class Task {

    @Id
    @Column(name = "id",unique = true,nullable = false)
    private Integer id;

    @Column(name = "comment_count")
    private Integer comment_count;

    @Column(name = "completed")
    private Boolean completed;

    @Column(name = "content")
    private String  content;

    @Column(name = "url")
    private String  url;

    @Column(name = "orderdata")
    private Integer orderdata;

    @Column(name = "priority")
    private Integer priority;

    @OneToOne
    @JoinColumn(name = "due_id")//this is relation table Due but this relation retunr response null
    private Due due;

 }

应有型号

  @Getter
  @Setter//Lombok library
  @Entity
  @Table(name = "due")
  public class Due {

    @Id
    @Column(nullable = false, unique = true, updatable = false)
    private Integer id;

   @Column(name = "date_data")
   private Date date_Data;

   @Column
   private Boolean recurring;

   @Column(name = "string_data")
  private String string_data;


}

Tasks Repository//这个反应性crud repositroy

     @Repository
     public interface TasksRepository extends ReactiveCrudRepository<Task,Integer> {

      }

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

   @Component
   @Slf4j
   @RequiredArgsConstructor
   public class  TaskManager {

          private final TasksRepository tasksRepository;

          public Flux<Task> findAll() {
           return tasksRepository.findAll();
          }

      }

任务服务//这是服务层

    @Service
    @RequiredArgsConstructor
    public class TasksService {


        private final TaskManager taskManager;


          public Flux<Task> getAll() {
            return taskManager.findAll();
      }
    }

任务控制器

  @RestController
  @RequestMapping("/api/tasks")
  @RequiredArgsConstructor
  public class TasksController {


        private final TasksService tasksService;

         @GetMapping
         public Flux<Task> getAllTasks(){
          return tasksService.getAll();
       }
     }

任务响应

我的休息Api响应任务

     {
        "id": 1,
        "comment_count": 10,
        "completed": false,
        "content": "new content",
        "url": "http:8085.com",
        "orderdata": 5,
        "priority": 5,
        "due": {
             "id": 3,
             "date_Data": null,
             "recurring": null,
             "string_data": null
 
      } 

问题一对一关系 Reponse null 什么问题???请帮助我 谢谢

错误跟踪

  2021-03-20 20:49:57.161 ERROR 13652 --- [actor-tcp-nio-1] 
   r.n.channel.ChannelOperationsHandler     : [id: 0x1126662b, 
    L:/127.0.0.1:55827 - R:localhost/127.0.0.1:5432] Error was received while 
     reading the incoming data. The connection will be closed.

    java.lang.WhosebugError: null
      at  io.netty.util.internal.ReferenceCountUpdater
     .isLiveNonVolatile(ReferenceCountUpdt 
     er.java:88) ~[netty-common-4.1.59.Final.jar:4.1.59.Final]
         at 
      io.netty.buffer.AbstractReferenceCountedByteBuf
      .isAccessible(AbstractReferenceCountedByteBuf.java:56) ~[netty-buffer- 
       4.1.59.Final.jar:4.1.59.Final]
       at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1455) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1385) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.UnsafeByteBufUtil.getBytes(UnsafeByteBufUtil.java:481) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.PooledUnsafeDirectByteBuf.getBytes(PooledUnsafeDirectByteBuf.java:130) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.PooledSlicedByteBuf.getBytes(PooledSlicedByteBuf.java:235) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.ByteBufUtil.decodeString(ByteBufUtil.java:1147) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.AbstractByteBuf.toString(AbstractByteBuf.java:1248) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.AbstractByteBuf.getCharSequence(AbstractByteBuf.java:515) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.netty.buffer.AbstractByteBuf.readCharSequence(AbstractByteBuf.java:520) ~[netty-buffer-4.1.59.Final.jar:4.1.59.Final]
at io.r2dbc.postgresql.util.ByteBufUtils.decode(ByteBufUtils.java:42) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.codec.AbstractNumericCodec.decodeNumber(AbstractNumericCodec.java:122) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.codec.AbstractNumericCodec.decodeNumber(AbstractNumericCodec.java:89) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.codec.IntegerCodec.doDecode(IntegerCodec.java:49) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.codec.IntegerCodec.doDecode(IntegerCodec.java:30) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.codec.AbstractCodec.decode(AbstractCodec.java:82) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.codec.DefaultCodecs.decode(DefaultCodecs.java:149) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.PostgresqlRow.decode(PostgresqlRow.java:90) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.postgresql.PostgresqlRow.get(PostgresqlRow.java:77) ~[r2dbc-postgresql-0.8.6.RELEASE.jar:0.8.6.RELEASE]
at io.r2dbc.spi.Row.get(Row.java:76) ~[r2dbc-spi-0.8.3.RELEASE.jar:na]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:173) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readEntityFrom(MappingR2dbcConverter.java:294) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]
at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.readFrom(MappingR2dbcConverter.java:166) ~[spring-data-r2dbc-1.2.5.jar:1.2.5]

好的,伙计,首先,为什么这个“Due”实体需要是它自己的实体? 我的意思是,一项任务可能有最后期限,并且可能会重复出现。保持简单。

关于代码,不考虑域建模方面:

使用 Long 或 UUID 作为 ID 字段类型。就个人而言,我从未在生产中见过 int。

为那些“到期”实体创建一个 CrudRepo,以检查您是否可以自行查询它们。写一个测试来验证。

我的猜测是“JoinTable”注释在某种程度上是多余的 and/or 配置错误。尝试使用“Due.ID”而不是“Due_ID”来告诉它应该使用 Due 对象的哪个字段。

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