0% found this document useful (0 votes)
25 views1 page

Relations Hibernate

The document discusses different types of relationships in JPA including OneToOne, OneToMany, and ManyToMany. It provides examples of how to annotate entities for each relationship type using JPA annotations like @OneToOne, @OneToMany, @ManyToMany, @JoinColumn, and @JoinTable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

Relations Hibernate

The document discusses different types of relationships in JPA including OneToOne, OneToMany, and ManyToMany. It provides examples of how to annotate entities for each relationship type using JPA annotations like @OneToOne, @OneToMany, @ManyToMany, @JoinColumn, and @JoinTable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

OneToOne:

for example if we take Customer and wallet,


in customer entity,
we have to take Wallet Object , and that should be annotate with @OneToOne
annotation
and need to provide mapped By attribute, and provide Wallet entity customer
Object reference

@OneToOne(mappedBy = "customer", cascade = CascadeType.ALL)


private Wallet wallet;

@OneToOne
@JoinColumn(name="fkcid")
private Customer customer;

-> then coming to wallet entity, and take customer Object


that should be annotate with @oneToOne annotation, and provide @joincolumn
annotation

we can provide, joincolumn annotation any side in OneToOne relation.

OneToMany:
-> in OneToMany relationship we have to take @joincolumn in many side
Customer and Orders it has one to many relationship

-> in Customer entity we have to take list of Orders, because we have


one customer have many orders

@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)


private Set<Orders> orders;

on the top of Object we have to annotate @OneToMany annotation


and provide attribute mapped By reference of customer Object in Orders Entity

-> go to the Orders entity and take Customer Object,


annotate with @manyToOne, and provide @JoinColumn column

ManyToMany:
-> in manyToMany relaionship we have to provide joincolumn in seperate table
-> Customer and Items have manyToMany relationship
-> in the Customer Entity we have to take List of Customer Object
and annotate with @manyToMany annotation,
and take Join tabele annotation, and provide join table name,
and join column and Inverse JoinColumn

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="customer_items_b3",
joinColumns= {@JoinColumn(name="cid")},
inverseJoinColumns= {@JoinColumn(name="itemId")})
private List<Items> items;

-> go to the Items entity and take List of Customer Object,


and annotate with @ManyToMany annotation

@ManyToMany
private List<Customer> customer;

You might also like