tags:

views:

115

answers:

2

Hello, I'm starting to work with hibernate. I'm quite confused about HQL joins... they are really different from, say, mySQL joins... In fact if I have two tables like Parking and Auto I would do:

select * from Parking left join on Parking.pid = Auto.parkingId

or something the like. Instead in hibernate I will have a Parking class already linked to Auto, and have all the associations mapped. So the joind would be automatically resolved by hibernate. Anyway I still have the join keyword but in the examples its used in a way I don't understand, usually specifying a property of the object in the from clause... what does it means? What is the SQL equiv of that operation?

+1  A: 

Let me try. Here's an example I've already given (take from here )

Imagine the case of an online shop which sells shirts. Each shirt model comes in a certain number of available sizes. You want a query to find all the shirt models with sizes over 40. In HQL, the query might be the following :

from Shirt shirt
join shirt.availableSizes size
where size.number > 40

In other words, you want to get all the objects, but you need the search criteria for some of the joined tables (and I mean join one-to-many - you have a collection of availableSizes and you can't reach the number property without the join).

edit
You can get some more info from here. There are some explanations about implicit and explicit joins. And more important - some fetching strategies (in case you put some connections between you tables as lazy but you also want, in some cases, to fetch them immediately). Read it, if you haven't, you might find it useful.

Danail
This way seems that the *classic* join I would do on a relational DB is already done. The only purpose of the join clause in HQL is to select a column of the joined table and put restrictions on it... isn't it?
gotch4
@gotch4 - well, not only this. See the edit, there is more explanations in the article I refer to.
Danail
+1  A: 

It's important to remember that HQL talks about classes and fields, whereas SQL talks about tables and columns.

And if you specify an association, hibernate will automatically do the joins for you. Imagine a class Library which contains a List of Book. Each book is in one library.

@Entity public class Library {
  @OneToMany(mappedBy"library")
  public List<Book> getBooks() {
    return books;
  }
  ...
} 

@Entity public class Book {
  @ManyToOne
  public Library getLibrary() {
    return library;
  }
  ...
}

Then, you can use hibernate to find a Library object for you, and you don't need to specify any joins. It just knows what to do (although you can often help it improve its performance if you give it more details).

Library library = entityManager.find(Library,1);
for (Book book : library.getBooks()) {
  System.out.println("Found book: " + book.toString());
}

In my experience, it's possible to get a lot done using hibernate without using "join" at all!

More details on the annotations at http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/

John