Pretty childish question, but I'm confused. I use Hibernate. He also creates tables. There is an entity user, there is an entity session. One user can have many sessions.

public class Session { @Id @Column(name = "id") @GeneratedValue() private long id; @Column(name = "token") private String token; @ManyToOne(fetch = FetchType.LAZY) private User user; } public class User { @Id @Column(name = "id") @GeneratedValue private long id; @Column(name = "name", nullable = false) private String name; @OneToMany(fetch = FetchType.LAZY) private List<Session> sessions; } 

Actually what's wrong. I want this structure to be generated in the database:

 Users(id, name) Sessions(id, token, userId) 

In general, then it happened. And even filled correctly. But additionally, another table is generated, which remains empty. With test creation and user login:

Table Users

enter image description here

Sessions Table

enter image description here

UserSessions table

enter image description here

Can someone chew me what's what? I basically understand why creating such a table at all. One user may (actually, no, but the hibernate does not know this) theoretically several sessions, and at the same time several users may have one session, ala space saving + database integrity. But why is it not filled then?

In order to understand both cases, how to make it (the UserSessions table) fill in and remove this userId from the Sessions table is interesting; and vice versa, how to remove the UserSessions table, leaving userId. And how did it happen for me that an unused table is being created?

    2 answers 2

    It's simple. Thus, you have two independent one-way links.

    One-to-many (user-> sessions) quite naturally creates a join-table.

    To create the reverse side of two-way communication, do the following:

     public class User { ... @OneToMany(mappedBy = "user") private List<Session> sessions; .... } 

    So JPA sees that this relationship is the reverse side of two-way communication. And the line is set in Session with the user attribute.

    At the same time, there will be no extra join-tables, for now it is known that the connection is held through the session.

      Understood. It was necessary to get into the documentation (on any training sites there is little sensible). I will not delete the question, it will suddenly come in handy to someone.

      In general, there are three situations.

      • Described by ManyToOne. Then only the foreign key is created. No additional tables are created.

      • Described by OneToMany. It is done through an additional table.

      • Described OneToMany and ManyToOne. At the same time, it works only through a foreign key. But! OneToMany must have MapedBy, otherwise the case above will work and an additional table will be created.

      Yet the question is interesting ... And why is that? How is OneToMany so fundamentally different from ManyToOne that without a table in any way? To which I myself logically found the answer) Again, maybe someone will be useful. With ManyToOne, we are in a table where there can be "many" objects describing a property referring to the "one" object. No extra table needed. But if we describe OneToMany, it turns out in the object "one" there should be a list of other objects. And this list is expressed in this very additional table. Like this.