0

I have read several examples about creating associations between tables with Hibernate and I have got a little bit confused. I want initilaly to know how many tables will be created in the database without specifying any annotation on the other side as with the code:

@Entity
public class Flight implements Serializable {
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn(name="COMP_ID")
    public Company getCompany() {
        return company;
    }
    ...
} 

I suspect two tables Flight, Company and the Flight contains a foreign key Company_Id. AM I right? What is the difference if I add the "mappedBy" on the other side as:

@Entity
public class Company {
    @OneToMany(mappedBy="company")
    public Set<Flight> getFlights() {
        ...
    }
}

How many tables will be created by the second approach? I suppose the second approach establishes a bidirectional association. What is practical difference between the two cases? What is going on also with the "mappedBy" annotation in the @ManyToMany association?

arjacsoh
  • 8,932
  • 28
  • 106
  • 166
  • You're asking what the difference is between the two cases, but you only present one case. What is the other case? Also, read this answer: http://stackoverflow.com/questions/16119531/hibernate-jpa-manytoone-vs-onetomany/16119715#16119715 – JB Nizet Apr 20 '13 at 15:13
  • The first case is without any annotation in the Company Entity and the second is with the annotation @OneToMany(mappedBy="company") as you see in the second code fragment. – arjacsoh Apr 20 '13 at 15:21

2 Answers2

1

If you didn't put any annotation on the flights property, the default mapping would apply, which is @Basic. That means that the whole contents of the list would be serialized and stored in a column named flights.

This is obviously not what you want. What you want is to make it the inverse side of the ManyToOne association defined in Flight. That's what @OneToMany(mappedBy = "company") does.

Other than that, your assumption is correct. A ManyToOne is mapped by default using a join column in the table of the entity. Using the JoinColumn annotation allows specifying a name other than the default one for the join column (as well as other properties of this join column).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • What do you mean by "whole contents of the list would be serialized and stored in a column named flights"? How is that depicted in the database? In order to understand I need the explanation about what is going on in the database. I would like also the same explanation for "the inverse side of the ManyToOne association defined in Flight". – arjacsoh Apr 20 '13 at 15:35
  • I mean that Java serialization would be used to transform the whole contents of the list to a byte array, and this byte array would be stored in a column named `flights` in the company table. Being the inverse side of the ManyToOne means that the two fields are actually the same association : a company C1 has the flights F1, F2, and F3, because F1, F2 and F3 have C1 as their company. A single join column in the Flight table is needed to map this bidirectional association. – JB Nizet Apr 20 '13 at 15:38
1

When your are using bidirectional relationship there is a change to store relationship in both tables by using mappedby you can avoid it. From your example if you remove mappedby, flight and company both tables will have relationship field.

Kanagaraj M
  • 956
  • 8
  • 18