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

rsl1atfo  于 12个月前  发布在  Spring
关注(0)|答案(4)|浏览(225)

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

@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//此React式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();
       }
     }


任务响应
我的Rest 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
 
      }

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

误差轨迹

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.StackOverflowError: 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]

fafcakar

fafcakar1#

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

bsxbgnwa

bsxbgnwa2#

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

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

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

@Getter
  @Setter
  @Entity
  public class NewDto { // depends on the relation you need

    @Column(name = "id")
    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;

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

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


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

bejyjqdl

bejyjqdl3#

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

wb1gzix0

wb1gzix04#

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

相关问题