Do I always need to create a separate class for the manytomany relationship in sleep mode

I read this article

http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example/

But he created three classes

Stock Category stock_category 

I always need a separate table for the relationship, or it can be done in 2 tables, and also only in the section "Promotions", "Category"

+4
source share
4 answers

No. Never. This is a common question that I have heard from many people. And the answer is that when creating objects:

You MUST NOT think in terms of relationships (RDBMS), but you must think in terms of objects (Java) .

In details. Hibernate is trying to achieve a rather difficult task: to connect an object-oriented Java Universe with Relational World. He achieves this in most cases at the cost of confusion between the two paradigms.

A better strategy (and less painful) is to design your objects and let Hibernate create a diagram for you. Let's pretend that

 Stock entity refers to many categories (Set of Categories) and Category entity has a set of Stock ids 

We have many-to-many relationships that can be moved from both objects. Here you need only two classes.

Then you need to annotate (here I assume you are using annotations, not .hbm.xml), these sets are like many-to-many attributes (read the Hibernate Annotation Guide ).

Compile the code. Then Configure Hibernate to create an automatic scheme (hbm2ddl.auto is a property that allows you to generate a scheme). If everything is in order, you will see tables 3 in the database tables 2 for entities and 1 table for many-to-many relationships (this is the third normal form of the scheme, for example, the cleanest).

Then save some objects and enjoy. And never forget Think in terms of OO when using hibernate (no JOIN operation) .

+2
source

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.

+4
source

You need a separate table for the connection in relation to man-many. However, you do not need a separate Java class representing this connection table.

In the cited code, the Stock and Category articles both have Set<StockCategory> fields associated with the @OneToMany annotation. Instead, you can use @ManyToMany annotations and completely avoid the StackCategory class. The table must still exist and will be mentioned in the @JoinTable annotation at one end of the relationship.

For more information on how to do this, see the docs in association associations .

0
source

You cannot, in general, have two tables that have foreign key restrictions on each other. Moreover, in each table you will get several almost duplicated rows, which will complicate their use in ways that are not related to the many-to-many relationship.

-1
source

Source: https://habr.com/ru/post/1342416/


All Articles