hibernate one to many. How to return the data from other (many)entity in one method but not in another

33 Views Asked by At

Lets say I have this class called Author

public class Author  {
    int id;
    String name;
    String email;
    String phoneNo;
    @OnetoMany List<Book>;
}

Now I have a method that needs to return only the author. I have another method that returns author and list of published books. I could make set list of books to null and return Author in the method that needs to return Author without list of published Books. But the problem is that would create an unnecessary select join on author and book tables and would fetch books when I do not need them. If I fetch lazy, I would not be able to return books in the other method where I need them in the response.

If I make another class Author without the list of books it would create duplicate code. What is the best practice to approach this problem?

0

There are 0 best solutions below