I do not understand how HQL joins

I'm starting to work with sleep mode. I'm pretty confused about HQLjoins ... they really are different from, say, mySQL joins ... Actually, if I have two tables, such as Parking and Auto, I would do:

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

or something similar. Instead, in sleep mode, I will have a parking class already associated with Auto and all association associations. Thus, the merge will automatically be enabled by hibernation. In any case, I still have the join keyword, but in the examples it is used in a way I don’t understand, usually indicating the property of an object in a from clause ... what does this mean? What is the SQL equiv of this operation?

+3
source share
2 answers

It is important to remember that HQL talks about classes and fields, while SQL talks about tables and columns.

And if you specify an association, hibernation will automatically make connections for you. Imagine a class library that contains a list of books. 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 the Library object for you, and you do not need to specify any connections. He just knows what to do (although you can often help him improve his performance if you give him more details).

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

In my experience, you can do a lot using sleep mode without using "join" at all!

Learn more about annotations at http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/

+2

. , ( )

, . . , 40. HQL :

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

, , ( ), availableSizes, " t number ).


. . - ( , , ). , , .

+2

Source: https://habr.com/ru/post/1729202/


All Articles