Bidirectional @OnetoMany and @ManyToOne on Quarkus with Panache

24 views Asked by At

I'm trying to implement a bidirectional @OnetoMany and @ManyToOne relationship between 2 entities for instance: Class AuthorEntity

@Entity
@Table(name = "author")
public class AuthorEntity extends PanacheEntityBase {
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<BookEntity> getBooks() {
        return books;
    }

    public void setBooks(Set<BookEntity> books) {
        this.books = books;
    }

    @Column(nullable = false)
    private String name;


    @OneToMany(mappedBy = "authorEntity", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private Set<BookEntity> books = new HashSet<>();


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void addbook(BookEntity book1) {
        books.add(book1);
    }
}

and Class BookEntity

@Entity
@Table(name = "book")
public class BookEntity extends PanacheEntityBase {
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(nullable = false)
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public AuthorEntity getAuthorEntity() {
        return authorEntity;
    }

    public void setAuthorEntity(AuthorEntity authorEntity) {
        this.authorEntity = authorEntity;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "author_entity_id", nullable = false)
    private AuthorEntity authorEntity;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}

and My unit test

@QuarkusTest
public class AuthorBookTest {


    @Test
    @Transactional
    public void testOneToManyRelationship() {
        // Create an author
        AuthorEntity author = new AuthorEntity();
        author.setName("John Doe");
        author.setId(Long.valueOf(3));
        author.persist();
        // Create books associated with the author
        BookEntity book1 = new BookEntity();
        book1.setTitle("Book 1");
        book1.setAuthorEntity(author);
        book1.setId(Long.valueOf(1));
        book1.persist();

        BookEntity book2 = new BookEntity();
        book2.setTitle("Book 2");
        book2.setAuthorEntity(author);
        book2.setId(Long.valueOf(2));
        book2.persist();

       
        // Refresh the author entity to load associated books
        AuthorEntity author2 = AuthorEntity.findById(author.getId());

        // Verify the relationship
        Assert.assertNotNull(author2);
        Assert.assertNotNull(author2.getBooks());
        Assert.assertEquals(2, author2.getBooks().size());
    }
}

My test will fail at the line Assert.assertNotNull(author2.getBooks());

if I add this 2 lines in the test before AuthorEntity author2 = AuthorEntity.findById(author.getId()) AuthorEntity author2 = AuthorEntity.findById(author.getId())

 author.addbook(book1);
 author.addbook(book2);

the test will not fail . why? Shouldn't it suppose populate the child records automatically ?

Thanks in advance for your help !

I've tried to change the fetch type on the the OneToMany annotation

0

There are 0 answers