Using Hibernate makes people model classes in this way
Class Author { @ManyToMany(mappedBy = "authors") private final List<Book> books = new ArrayList<>(); } Class Book { @ManyToMany private final List<Author> authors = new ArrayList<>(); }
The advantage of this modelling is that it allows you to write code like this
author = (Author) session.get(Author.class,1); author.getBooks()
If you don't model your classes in a Hibernate-OO way, most probably, your classes will look like this.
Class Author { } Class Book { private int authorID; }
Ignore the improper normalization for now (i.e I undersand that A book can have many authors so there will be multiple books records for the same Book in the Book table)
In such a model, the classes are mirror images of the relational model. The business logic is as follows
int authorID = 1; List<Books> = getBooksByAuthorID(authorID) // this would be HQL/criteria
In the above approach, Hibernate is not being leverages in the business logic, there is no modelling done that helps pick books directly from the authorID. The query has to be explicitly written (getBooksByAuthor). This query is not "modelled" inside the entity by annotations and not generated by Hibernate. Rather, it is controlled by Dao and DaoImpl layers.
No comments:
Post a Comment