Why use FROM with Hibernate?

1

I have been working with Hibernate for some time and I do not understand why they are complicated when making bbdd queries:

Root<Foo> fooRoot = criteriaQuery.from(Foo.class);
Join<Foo, Foo1> foo1 = fooRoot.join("foo1", JoinType.LEFT);

When you can simply:

criteriaQuery.from(Foo.class).list();

What advantages does it bring to create the Join by hand when you already have defined in your configuration file or annotations how the entities of your model are related. If it is for performance, hibernate does not recover the data until you expressly consent to that data.

Thanks for the answers.

    
asked by Zawarudo 26.05.2017 в 11:59
source

1 answer

0

First, technically it is not Hibernate, it is JPA (although you use Hibernate as an implementation, it has to work with any other JPA implementation).

Regarding the issue, you are comparing two different situations; with the first you have a reference to the entity Foo1 that you can use to add restrictions.

Root<Foo> fooRoot = criteriaQuery.from(Foo.class);
Join<Foo, Foo1> foo1 = fooRoot.join("foo1", JoinType.LEFT);
ParameterExpression<Integer> parameterExpression = criteriaBuilder.parameter(Integer.class);
criteriaQuery.where(criteriaBuilder.gt(foo1.get("atributoDeFoo1"), 50));

If you do not have to filter by the navigated entity, you do not need Join .

    
answered by 26.05.2017 / 12:22
source