You have a many-to-many relationship, you will need a connection database table, in your case, Stock_Category . However, this join table may not appear as a Hibernate object, depending on what you are trying to execute.
If your connection table contains only primary keys from the Stock table and the Category table, you can use @ManyToMany . In this case, the stock object will have stock.getCategories() , which returns all categories, and in the Category element there will be category.getStocks() , which returns all stocks. The connection table is not displayed at all.
However, if you intend to have additional behaviors in the Stock_Category table, for example, if you want someone to assign stock to a category or when it was added, you will need to use @OneToMany and @ManyToOne instead of @ManyToMany . In this case, the connection table will be displayed as a sleep object. That way, the stock object will have stock.getStockCategories() , which returns all stock categories, while the category object can also have stock.getStockCategories() (if you set up a bidirectional relation), which returns the same. The StockCategory object contains additional properties, such as personWhoAddsTheStockIntoCategory or dateAdded , etc.
Hope this helps.
source share