0

I am new to hibernate, trying to get information from more than 2 tables using hql, if we pull that information we need to put in a POJO how to do mapping for each column to the information that we get as a result from the query?

Tried using @secondarytable() and @Table but it cannot allow more than 1 secondary table annotation.

So is it that we cannot have join for more than 2 tables in hibernate?

piyush
  • 115
  • 2
  • 12

1 Answers1

1

You would want to use the @OneToMany, @ManyToOne, and/or @ManyToMany annotations to map relationships between all of the entities in your project.

The oracle documentation for those has some pretty good examples, like this one:

Example 1: One-to-Many association using generics

In Customer class:

@OneToMany(cascade=ALL, mappedBy="customer")
public Set<Order> getOrders() { return orders; }

In Order class:

@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }

In this example, both Customer and Order would be classes annotated with @Entity and @Table that correspond to CUSTOMER and ORDER tables in your database. Any other columns in those tables would also be mapped in the corresponding entity class. The @OneToMany, @ManyToOne and @ManyToMany are used to map foreign keys between your tables, and allow you to have direct references to parent/child rows/objects directly from your entity instances.

With these annotations, you can map as many foreign key references to other tables as you need.

Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102