what if we do not specify mappedBy attribute in OneToMany annotation?
hibernate, java, jpa
Solution
Because if you don't specify `mappedBy`, you're not saying that the `OneToMany` between `Item` and `Bid` and the `ManyToOne` between `Bid` and `Item` are actually the two sides of a unique bidirectional association.
So Hibernate considers that they are two, different, unidirectional associations. And since the default mapping for a `OneToMany` association is to use a join table, that's what Hibernate uses.
Problem
I am learning JPA with hibernate these days. I am not able to understand why hibernate gives error for a Bidirectional OnetoMany relationship if mappedBy attribute is not specified. Following is the code on which I am getting error: ``` @Entity public class Item { @Id private long id; private String name; private String description; @OneToMany() Set<Bid> bids = new HashSet<Bid>(); ``` Bid is the child entity of ITEM ``` @Entity(name="BIDS") public class Bid { @Id @Column(name="BID_ID") private Long id; private Double amount; @ManyToOne @JoinColumn(name="ITEM_ID") Item item; ``` In the main class I am saving both parent and child entities: ``` Transaction transaction = session.beginTransaction(); Item item = new Item(); item.setId(111); item.setDescription("ITEM Description"); item.setName("Name1"); Bid bid = new Bid(); bid.setId(21l); bid.setAmount(1.1); bid.setItem(item); Set<Bid> bids = new HashSet<Bid>(); bids.add(bid); item.setBids(bids); session.save(item); session.save(bid); transaction.commit(); ``` But hibernate tries to execute following queries and throws excetion that ITEM_BIDS table do not exist. ``` Hibernate: insert into Item (description, name, id) values (?, ?, ?) Hibernate: insert into BIDS (amount, ITEM_ID, BID_ID) values (?, ?, ?) Hibernate: insert into Item_BIDS (Item_id, bids_BID_ID) values (?, ?) ``` Please tell me why hibernate is generating extra query and how mappedBy element will solve this.