4

I try create a simple web application with database. My database has only 2 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 receve error: enter image description here

Please help what I'm doing wrong

Logan_on
  • 117
  • 4
  • 11

1 Answers1

3

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.

TomStroemer
  • 1,390
  • 8
  • 28
  • hello would u help me with lazy initialize?When i tried use my Rest method getAll() i received error https://imgur.com/a/04YzGIA – Logan_on Jun 16 '20 at 08:13
  • Please create a new question. Add your complete stacktrace as text and not as image. Please also share the code where you call the method and the relevant parts of your entity. – TomStroemer Jun 16 '20 at 09:11