Quarkus/Hiberante 2 个表之间的关系

Quarkus/Hiberante relationship between 2 tables

我尝试使用数据库创建一个简单的 Web 应用程序。我的数据库只有 2 列:

CREATE TABLE IF NOT EXISTS `Board` (
  `BoardID` Integer NOT NULL AUTO_INCREMENT,
  `Title` Varchar(255),
  `Position` Integer NOT NULL,
  PRIMARY KEY (`BoardID`)
);

CREATE TABLE IF NOT EXISTS `Task` (
  `TaskID` Integer NOT NULL AUTO_INCREMENT,
  `BoardID` Integer NOT NULL,
  `Title` Varchar(255),
  `Description` Varchar (1000),
  PRIMARY KEY (`TaskID`),
  FOREIGN KEY (`BoardID`)
  REFERENCES Board(`BoardID`)
);

型号:

@Entity
public class Task extends PanacheEntity {

    @Column(name = "TaskID")
    private Long taskId;

    @ManyToOne (fetch = FetchType.LAZY)
    @JoinColumn(name = "BoardID")
    private Board board;
...
}
@Entity
public class Board extends PanacheEntity{

    @Column(name = "BoardID")
    private Long boardId;

    @OneToMany(mappedBy = "board", orphanRemoval = true)
    private Set<Task> task;
...
}

我的 REST 方法

@Path("/hello")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ExampleResource {

    @Inject
    BoardRepository boardRepository;

    @GET
    @Transactional
    public List<Board> getAll() {
        return boardRepository.listAll();
    }
}

我的代码正在编译,但是当我调用我的 REST 方法时我收到错误: enter image description here

请帮助我做错了什么

检查 PanacheEntity 的代码。来自 JavaDoc:

Represents an entity with a generated ID field {@link #id} of type {@link Long}. If your Hibernate entities extend this class they gain the ID field and auto-generated accessors to all their public fields (unless annotated with {@link Transient}), as well as all the useful methods from {@link PanacheEntityBase}.

If you want a custom ID type or strategy, you can directly extend {@link PanacheEntityBase} instead, and write your own ID field. You will still get auto-generated accessors and all the useful methods.

从此处复制的 JavaDoc:https://github.com/quarkusio/quarkus/blob/master/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/PanacheEntity.java

您的 ID 列没有名称 'id'。所以你应该改用 PanacheEntityBase 并且你必须更改你的实体并将 @Id 注释添加到你的 id 字段:

@Entity
public class Task extends PanacheEntityBase {

    @Id
    @Column(name = "TaskID")
    private Long taskId;

    @ManyToOne (fetch = FetchType.LAZY)
    @JoinColumn(name = "BoardID")
    private Board board;
...
}
@Entity
public class Board extends PanacheEntityBase {

    @Id
    @Column(name = "BoardID")
    private Long boardId;

    @OneToMany(mappedBy = "board", orphanRemoval = true)
    private Set<Task> task;
...
}

如果您想使用 PanacheEntity 作为您的基础 class 您必须更改数据库中的列名并从您的实体中删除 taskId 和 boardId。