0

I am new in hibernate I gone through few(1,2) tutorials on hibernate and even implement in my project,I reached the following conclusion-

mapped by means other side of object is owner

This is just for database point of view,how actually I want to create relation ship in database,In general mapped by is used to avoid unnecessary table.

From java point of view this has no impact on your code,same code which running with mapped by will behave exactly same even we remove mapped by.

Is my above understanding is correct,if not can you please explain in which case this behave differently?

Community
  • 1
  • 1
TheCurious
  • 593
  • 1
  • 4
  • 29

2 Answers2

5

No, it's not. mappedBy has actually nothing to do with the database. It tells which side of the association is the owning side, when the association is bidirectional. Suppose you have an association between a Company (inverse side) and an Employee (owner side). When establishing an association between google and john,

google.addEmployee(john);

will NOT make Hibernate save the association in the database, because you only set the inverse side of the association.

john.setCompany(google);

will make Hibernate save the association in the database, because you set the owner side of the association.

Every bidirectional association MUST have an owner side and an inverse side.

In a OneToMany bidirectional association, the owner side MUST be the many side.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I must be try this by own but just want to know at a moment , if I enable cascading than `google.addEmployee(john)` will work or not? – TheCurious Feb 19 '17 at 22:26
  • 1
    No. With a cascase PERSIST being set, for example, persisting google will persist john, but john still won't belong to google. – JB Nizet Feb 19 '17 at 22:28
  • as u said **Every bidirectional association MUST have an owner side and an inverse side.** but i have persisted bidirectional without `mapped by` but it just created additional table in case of `@onetomany` and `@manytomany` and joincolum in both table, in case of `@onetoone` . Is this just a best practice for better db design? – TheCurious Feb 20 '17 at 11:00
  • 2
    If you didn't have a mappedBy, then you actually had two unidirectional associations (that could very easily become inconsistent). You don't want that. – JB Nizet Feb 20 '17 at 11:11
  • thank you so much,I just want to see that inconsistency,how I can I replicate it,can you please guide or demonstrate?I have a [similar](http://stackoverflow.com/questions/42340364/hbernate-unideirection-onetoone-with-cascade-enable) question on it. – TheCurious Feb 20 '17 at 11:28
  • 2
    save a company and add an employee to it, without setting the employee's company. Then, in another transaction, load the employee and ask what its company is. It will return null instead of the company. Load the company and check its collection of employees: the employee should be there. – JB Nizet Feb 20 '17 at 11:49
0

It's easy to understand, mappedBy shows where FK resides and as JB Nizet sad it shows who is the owner of relationship, who is responsible for managing relationship.

idmitriev
  • 4,619
  • 4
  • 28
  • 44
  • It doesn't always show where the FK resides. In many-to-many associations, for example, mappedBy is needed, but doesn't refer to where the FK is. That's not the right way to think about mappedBy. – JB Nizet Feb 20 '17 at 14:22