0

Recently, I have been learning about Hibernate, and I am facing some difficulties. My first problem is as follows: I am very much confused with the below terms.

  1. Bidirectional mapping
  2. Many to One

Because, as far as I know, in rdbms we first need to insert in parent table. Then we can insert on child table, so the only possible scenario is one-to-many (first parent then children). Then, how is many-to-one is going to work? Second, what is this bidirectional mapping in regards to Hibernate. Specifically, different types of join annotations confuse me a lot. I am listing those annotations below.

1.@JoinTable(name = "Tbale_Name", joinColumns = { @JoinColumn(name = "Column_Name") }, 
    inverseJoinColumns = { @JoinColumn(name = "Another_ColumnName") })

2.@OneToMany(mappedBy="department")` this mappedby term 

3.@PrimaryKeyJoinColumn

Please help me understand these concepts.

NaN
  • 683
  • 1
  • 8
  • 15

1 Answers1

1

The first thing I would say is don't think in terms of tables but think in terms of Objects.

What you are trying to express with the annotations is the relationship between objects, let hibernate work out how to persist the data. You can obviously manually check the SQL but the idea of using an ORM is to map the relationships between entities accordingly and let the ORM figure out the complexity around generating SQL etc.

Its worth noting that the parent -> child relation can be mapped using @ManyToOne by adding mappedBy to the non-owning (child) side of the relationship. Hibernate then will determine which entities to insert into the database first. Running with a TransactionManager will enforce integrity with multi table inserts. Hibernate will also workout which entities need to be persisted, for example if you add an new object on the many side to an existing object on the one side.

Furthermore, its worth understanding that in some cases it won't always be the database that generates the primary key in a parent -> child foreign key. Its possible for the code to generate the Identifier and hibernate will persist them according.

Bidirectional mapping means that object entities have a reference to each other. i.e. You can retrieve the second entity from the first entity. Bidirectional mapping supports one-to-many or many-to-many. I.e. OneToMany = a Set on one of the entities. Many-To-Many = Sets on both entities.

JoinTable tells hibernate that a table in the database can be used to map to other tables together. See JPA "@JoinTable" annotation for more information. JoinColumn tells hibernate what column to use to make the join between the two entities. Hibernate needs these to construct the SQL.

Community
  • 1
  • 1
Mark Butler
  • 466
  • 3
  • 10
  • you are right i have to think in term of objects that's why hibernate is an orm, say i have two entity User and Email, so it's possible to have more than one email for one user, to search email(s) for a particular user i need User information First, then if i don't use Bidirectional mapping i wont be able to " retrieve the second entity from the first entity "? – NaN Oct 17 '13 at 07:11
  • That's a unidirectional OneToMany relationship, being 'User' the owner. If you want to get the emails of a user, get the collection. I.e: user.getEmails() – mrod Oct 17 '13 at 08:03