Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quarkus/Hibernate relationship between two tables

I am trying create a simple web application with a database. My database has only two columns:

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`)
);

Models :

@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;
...
}

My REST method:

@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();
    }
}

My code is compiling, but when I call my REST method I receive the following error:

enter image description here

like image 862
Logan_on Avatar asked Sep 21 '25 05:09

Logan_on


1 Answers

Check the code of PanacheEntity. From the 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 copied from here: https://github.com/quarkusio/quarkus/blob/master/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/PanacheEntity.java

Your id-columns don't have the name 'id'. So you should use PanacheEntityBase instead and you have to change your entities and add the @Id annotation to your id fields:

@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;
...
}

If you want to use PanacheEntity as your base class you have to change your column names in the database and remove the taskId and boardId from your entities.

like image 93
TomStroemer Avatar answered Sep 22 '25 22:09

TomStroemer