3

I am new to Hibernate. I have a OneToMany relationship with bidirectional mapping between Account and Transaction. I am not using @JoinColumn in either class and using @mappedBy in the non owning Account class. And everything is working fine. Using H2 in memory database, new join column is created in Transaction table. Then what is the use of @JoinColumn in OneToMany relationships? Is it for unidirectional mapping only? Below is the code. I also read for reference JPA JoinColumn vs mappedBy

public class Account {
    @OneToMany( mappedBy="account", cascade=CascadeType.ALL)
    List<Transaction> list= new ArrayList<Transaction>();
}
    
public class Transaction {    
    @ManyToOne
    Account account;
}

Application class :

Account a = new Account("savings");
        
Transaction t1 = new Transaction("shoe purchase", 45);
        
t1.setAccount(a);
        
a.getList().add(t1);
        
accountRepository.save(a);

output:

Transaction table has an entry with foreign key which is account number in that one row in Account table. ACCOUNT_ID column in created in Transaction table.

There are no extra tables created.

Archmede
  • 1,592
  • 2
  • 20
  • 37
Ben G
  • 55
  • 8

3 Answers3

1

Use of

mappedBy

is instruct framework to enable bi-directional relationship. Because of @ManyToOne on Transaction class you Your Transaction Table will have foreign key referring to Account table primary key. By default, Hibernate generates the name of the foreign key column based on the name of the relationship mapping attribute and the name of the primary key attribute. In this example, Hibernate would use a column with the name account_id to store the foreign key to the Account entity.

@JoinColum

can be used If you would like override default foreign key name like @JoinColum(name="acc_id")

sandeep pandey
  • 350
  • 1
  • 10
1

Jpa works on the idea of configuration by convention. So, it will perform configuration on your behalf whenever it can. Think of the @Column annotation, you don't have to apply it on every entity attribute, you would need it only when you have to change something about the attributes.

It's the same with @JoinColumn, when you added @ManyToOne, Jpa already knows that you will need the join column and thus was added for you and the default naming convention for the foreign key was applied (attributename_primarykeyoftheothertype).

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • Got it, thanks! Same turned out to be the case for ManyToMany - I only had mappedBy and no JoinColumn but a separate join table was created which maintained the relationship alone. So believe there also JPA figured it out. Yes as per another answer, I could use JoinColumn to change the column name , but the relationship itself was established using mappedBy alone. So only if it was Unidirectional, JoinColumn would be required I am conclude. – Ben G Feb 23 '19 at 05:00
0

MappedBy intructs Hibernate that the key used for the association is on the other side of the association.Like in this case ACCOUNT_ID is being created on Account table.

That means that although you associate two tables together, only one table is having foreign key constraint to the other one.

MappedBylets you to still associate from the table not having foreign key constraint to the other table.

  • In this case Account_Id is created in the Transaction table. There is alter table transaction add constraint FK6g20fcr3bhr6bihgy24rq1r1b foreign key (account_id) references account. From what I know so far, this is the only constraint that is required for it to work. – Ben G Feb 23 '19 at 05:13
  • That you have to do manually if you do not use Hibernate or any other ORM. Hibernate does that internally on the behalf of you that's the beauty. – kamlesh pandey Feb 23 '19 at 05:31