I have a one-to-many relationship between the tables, I use the @JoinColumn annotation, as I understand this annotation simply adds an extra column with an identifier to one of the tables, but I have an additional intermediate table, I would like to ask why. The first entity:

 @Entity @Table(name = "TRANSACTION") public class Transaction { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column private Long TRANSACTION_ID; @ManyToOne @JoinColumn(name = "ACCOUNT_ID") private Account account; 

The second entity:

 @Entity @Table(name = "ACCOUNT") public class Account { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column private Long ACCOUNT_ID; @OneToMany(cascade = CascadeType.ALL) List<Transaction> transactions = new ArrayList<>(); 

enter image description here

enter image description here

    1 answer 1

    The use of the @JoinColumn annotation @JoinColumn needed in order to specify which column of the table will be used when setting up a JOIN connection in an SQL query when selecting data to be placed in this object. In the @OneToMany and @ManyToOne , the intermediate table is not used. And it is used in the case of @ManyToMany relations. If the table is not used, then it can be safely deleted or the database schema can be regenerated using hbm2ddl.auto=create .