Hibernate Criteria query doesnt query the many in one to many - java

I am trying to create a one to many query. the join seems to be working because the set gets populated but when i add a query to the set it doesn't seem to do anything
Here is one class:
#OneToMany(fetch=FetchType.LAZY, mappedBy="parent")
public Set<Child> getChildren() {
return this.children;
}
Here is the other
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="PARENT_ID")
public Parent getParent() {
return this.parent;
}
and here is the not working query
Criteria criteria = session.createCriteria(Parent.class,"p")
.setFetchMode("p.children", FetchMode.JOIN)
.createAlias("p.children", "c")
.add(Restrictions.like("p.name", "%" + nameQuery + "%").ignoreCase())
.add(Restrictions.eq("c.gender", "boy"));
and this query gets me all my parents by name correnctly and their all children, but i only want the boy children and yet it gives me all the girls too. anyone see what i am doing wrong?

As per the docs,
Join fetching: Hibernate retrieves the associated instance or collection
in the same SELECT, using an OUTER JOIN.
In an outer join, you will get all the records in both the tables irrespective of the join condition, so change your fetching strategy to SELECT and it should fetch only the results matching the restriction. Also I'd recommend to enable batch fetching if you're not doing that already.

Related

Hibernate left join fetch by non-primary keys

Following entities:
#Table
class AA1 {
#Id
Long id;
String a_number;
Category category;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name= 'a_number', referencedColumnName='a_number')
#JoinColumn(name= 'category', referencedColumnName='category')
BB1 bb1
...other fields...
}
#Table
class BB1 {
#Id
String a_number;
Category category;
String value;
}
JPQL query:
SELECT a FROM AA1 a LEFT JOIN a.bb1 b;
Hibernate produces correct sql query, but when it tries to collect data it makes additional call like:
SELECT b.a_number, b.category, b.value FROM BB1 b WHERE b.a_number = ? AND b.category = ?
I checked that query returns null.
How can I avoid such database queries?
My investigation: As far as I see Hibernate creates key by(AA1.a_number and AA1.category) and tries to retrieve entity from context. And for specific row 'left join' query returns null values and Hibernate asks context by key and context returns null, it leads to call to database for it.
You must add FETCH to your JPQL query :
SELECT a FROM AA1 a LEFT JOIN FETCH a.bb1 b;
but keep the LAZY loading, because Hibernante will always try to get ManyToOne or OneToOne relationship, which are EAGER by default, with an additional query.
Look at this article https://thorben-janssen.com/5-common-hibernate-mistakes-that-cause-dozens-of-unexpected-queries/ from Th
By default, to make lazy loading work for #OneToOne and #ManyToOne, you need to enable "no proxy lazy associations". Otherwize, despite the FetchType.LAZY annotation, the associated object will be "fetched" and the "fetch" will be done with an extra sql query.
Therefore, one half-way solution to leverage performances without enabling "no proxy lazy associations" is to avoid extra queries by forcing a join fetch on the associated objet. Various technics allow to reach this goal : "LEFT JOIN FETCH" in JPQL queries or EntityGraph.
Just looking at the entity definition #ManyToOne(fetch = FetchType.LAZY) is the cause.
You are explicitly telling JPA to fetch BB1 only when it is needed/accessed.Hence when the first call to get the parent entity is made BB1 is not loaded.It is only when you are accessing the child that triggers JPA to fetch it.
If you change it to FetchType.EAGER , both the entities will be queried in a single call.
But be careful there are advantages/pit-falls with either approach.
Read more abt it here : https://thorben-janssen.com/entity-mappings-introduction-jpa-fetchtypes/

How to remove all SubcriteriaList from projectionCriteria in Hibernate (Java) - prevent from using left join

I am working with Hibernate and dto,dao design patterns (Java).
i have an entity class and attribute in it and they are defined with #ManyToOne annotation.
I would like to create a count query and "tell" hibernate "DO NOT JOIN" with #ManyToOne tables
While creating a count query:
(Long) crit.setProjection(Projections.count("id")).uniqueResult();
The sql exceute by hibernate is with left join in it .
Even if i count explicitly on the #Id annotation from the entity class .
The actual query appear is with LEFT JOIN for all the "other tables".
That SQL query build by Hibernate - is not efficient since there is no reason for creating a left join when #ManyToOne is set.
After trying and reading about hibernate i found out about FetchMode
but even when setting FetchMode.LAZY
.setFetchMode("brand", FetchMode.LAZY)
The Sql from hiberante having left join in it.
i have also attached the photo from debug that showing all the SubcriteriaList
which is under projectionCriteria .
How could i tell Hibernate DON'T left join Tables from entity class ?
(without writing SQL query by myself) ?
public Response findAll() {
Criteria crit = getDtoCriteria();
}
public Criteria getDtoCriteria() {
return getDtoCriteria(getDtoClass(), getSession());
}
public Criteria getDtoCriteria(Class clazz, Session session) {
Criteria crit = createEntityCriteria(session);
setProjecttionForDto(crit, true, clazz);
return crit;
}
This all are my Subcriteria
[Subcriteria(bran*******ance:bran*******ance), Subcriteria(buc*****:buc*****), Subcriteria(br****:br****), Subcriteria(dyn***:dyn***), Subcriteria(dyna*****.user:user)]
Try using FetchMode.SELECT instead:
(Long) crit.setFetchMode("brand",FetchMode.SELECT)
.setProjection(Projections.count("id"))
.uniqueResult();

How to use multiple JOIN FETCH in one JPQL query

I have below entities:
public class Category {
private Integer id;
#OneToMany(mappedBy = "parent")
private List<Topic> topics;
}
public class Topic {
private Integer id;
#OneToMany(mappedBy = "parent")
private List<Posts> posts;
#ManyToOne
#JoinColumn(name = "id")
private Category parent;
}
public class Post {
private Integer id;
#ManyToOne
#JoinColumn(name = "id")
private Topic parent;
/* Post fields */
}
and I want to fetch all categories with joined topics and joined posts using JPQL query. I wrote query like below:
SELECT c FROM Category c
JOIN FETCH c.topics t
JOIN FETCH t.posts p WHERE
But I got the error
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
I found articles about this error, but these articles only describe situation where in one entity are two collections to join. My problem is a little different and I don't know how to solve it.
It is possible to do it in one query?
Considering we have the following entities:
And, you want to fetch some parent Post entities along with all the associated comments and tags collections.
If you are using more than one JOIN FETCH directives:
List<Post> posts = entityManager.createQuery("""
select p
from Post p
left join fetch p.comments
left join fetch p.tags
where p.id between :minId and :maxId
""", Post.class)
.setParameter("minId", 1L)
.setParameter("maxId", 50L)
.getResultList();
Hibernate will throw the MultipleBagFetchException:
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags [
com.vladmihalcea.book.hpjp.hibernate.fetching.Post.comments,
com.vladmihalcea.book.hpjp.hibernate.fetching.Post.tags
]
The reason why Hibernate throws this exception is that it does not allow fetching more than one bag because that would generate a Cartesian product.
The worst "solution" others might try to sell you
Now, you will find lots of answers, blog posts, videos, or other resources telling you to use a Set instead of a List for your collections.
That's terrible advice. Don't do that!
Using Sets instead of Lists will make the MultipleBagFetchException go away, but the Cartesian Product will still be there, which is actually even worse, as you'll find out the performance issue long after you applied this "fix".
The proper solution
You can do the following trick:
List<Post> posts = entityManager.createQuery("""
select distinct p
from Post p
left join fetch p.comments
where p.id between :minId and :maxId
""", Post.class)
.setParameter("minId", 1L)
.setParameter("maxId", 50L)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();
posts = entityManager.createQuery("""
select distinct p
from Post p
left join fetch p.tags t
where p in :posts
""", Post.class)
.setParameter("posts", posts)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();
In the first JPQL query, distinct DOES NOT go to the SQL statement. That's why we set the PASS_DISTINCT_THROUGH JPA query hint to false.
DISTINCT has two meanings in JPQL, and here, we need it to deduplicate the Java object references returned by getResultList on the Java side, not the SQL side.
As long as you fetch at most one collection using JOIN FETCH, you will be fine.
By using multiple queries, you will avoid the Cartesian Product since any other collection but the first one is fetched using a secondary query.
Always avoid the FetchType.EAGER strategy
If you're using the FetchType.EAGER strategy at mapping time for #OneToMany or #ManyToMany associations, then you could easily end up with a MultipleBagFetchException.
You are better off switching from FetchType.EAGER to Fetchype.LAZY since eager fetching is a terrible idea that can lead to critical application performance issues.
Conclusion
Avoid FetchType.EAGER and don't switch from List to Set just because doing so will make Hibernate hide the MultipleBagFetchException under the carpet. Fetch just one collection at a time, and you'll be fine.
As long as you do it with the same number of queries as you have collections to initialize, you are fine. Just don't initialize the collections in a loop, as that will trigger N+1 query issues, which are also bad for performance.
Here is a working example of complex join and multiple consition:
String query_findByProductDepartmentHospital = "select location from ProductInstallLocation location "
+ " join location.product prod " + " join location.department dep "
+ " join location.department.hospital hos " + " where prod.name = :product "
+ " and dep.name.name = :department " + " and hos.name = :hospital ";
#Query(query_findByProductDepartmentHospital)
ProductInstallLocation findByProductDepartmentHospital(#Param("product") String productName,#Param("department") String departName, #Param("hospital") String hospitalName);
A workaround is to use #Query and #EntityGraph together, like it mentioned here use #Query and #EntityGraph together

How to build HQL query, thats joins subtables marked LAZY, automatically?

I have some entity:
public class Album extends GenericAuditedEntity {
#OneToMany(fetch = FetchType.LAZY)
private Set<Item> itemSet = new HashSet<Item>();
}
And when i run HQL like this:
em.createQuery("select a from Album a").getResults()
it produses many SQL queries:
One for select data from Album's table. Smth like this: select .... from Album_table;
And one query for each fetched row, for selecting items. Smth like this:
select .... from Item_table iwhere i.Album_id = :Album_id;
But when i run em.createQuery("
select a.id, b.id
from Album a
left join Item i
").getResults()
it produses one SQL query. But it's result is list of some parameters, that i need put into the entities manually.
How can i build HQL with joins automatically and automatically put the results to the entities? Is it possible?
You need to use join fetch:
em.createQuery("select a.id, b.id from Album a left join fetch Item i ").getResults();
Note that there are certain side effects to that, described in detail the above link.
If you are using join fetch then you don't need the IDs, you can retrieve the Entity as Hibernate will also populate the association in it's first-level cache
em.createQuery("select a from Album a left join fetch a.itemSet").getResultList();
However if you are retrieving the IDs but want populated Objects/Entities then consider using a Constructor
em.createQuery("select new com.xxx.AlbumItem(a.id, b.id) from Album a left join fetch a.itemSet b").getResultList();
dont use lazy fetching. set fetch type to eager

JPA eager fetch does not join

What exactly does JPA's fetch strategy control? I can't detect any difference between eager and lazy. In both cases JPA/Hibernate does not automatically join many-to-one relationships.
Example: Person has a single address. An address can belong to many people. The JPA annotated entity classes look like:
#Entity
public class Person {
#Id
public Integer id;
public String name;
#ManyToOne(fetch=FetchType.LAZY or EAGER)
public Address address;
}
#Entity
public class Address {
#Id
public Integer id;
public String name;
}
If I use the JPA query:
select p from Person p where ...
JPA/Hibernate generates one SQL query to select from Person table, and then a distinct address query for each person:
select ... from Person where ...
select ... from Address where id=1
select ... from Address where id=2
select ... from Address where id=3
This is very bad for large result sets. If there are 1000 people it generates 1001 queries (1 from Person and 1000 distinct from Address). I know this because I'm looking at MySQL's query log. It was my understanding that setting address's fetch type to eager will cause JPA/Hibernate to automatically query with a join. However, regardless of the fetch type, it still generates distinct queries for relationships.
Only when I explicitly tell it to join does it actually join:
select p, a from Person p left join p.address a where ...
Am I missing something here? I now have to hand code every query so that it left joins the many-to-one relationships. I'm using Hibernate's JPA implementation with MySQL.
Edit: It appears (see Hibernate FAQ here and here) that FetchType does not impact JPA queries. So in my case I have explicitly tell it to join.
JPA doesn't provide any specification on mapping annotations to select fetch strategy. In general, related entities can be fetched in any one of the ways given below
SELECT => one query for root entities + one query for related mapped entity/collection of each root entity = (n+1) queries
SUBSELECT => one query for root entities + second query for related mapped entity/collection of all root entities retrieved in first query = 2 queries
JOIN => one query to fetch both root entities and all of their mapped entity/collection = 1 query
So SELECT and JOIN are two extremes and SUBSELECT falls in between. One can choose suitable strategy based on her/his domain model.
By default SELECT is used by both JPA/EclipseLink and Hibernate. This can be overridden by using:
#Fetch(FetchMode.JOIN)
#Fetch(FetchMode.SUBSELECT)
in Hibernate. It also allows to set SELECT mode explicitly using #Fetch(FetchMode.SELECT) which can be tuned by using batch size e.g. #BatchSize(size=10).
Corresponding annotations in EclipseLink are:
#JoinFetch
#BatchFetch
"mxc" is right. fetchType just specifies when the relation should be resolved.
To optimize eager loading by using an outer join you have to add
#Fetch(FetchMode.JOIN)
to your field. This is a hibernate specific annotation.
The fetchType attribute controls whether the annotated field is fetched immediately when the primary entity is fetched. It does not necessarily dictate how the fetch statement is constructed, the actual sql implementation depends on the provider you are using toplink/hibernate etc.
If you set fetchType=EAGER This means that the annotated field is populated with its values at the same time as the other fields in the entity. So if you open an entitymanager retrieve your person objects and then close the entitymanager, subsequently doing a person.address will not result in a lazy load exception being thrown.
If you set fetchType=LAZY the field is only populated when it is accessed. If you have closed the entitymanager by then a lazy load exception will be thrown if you do a person.address. To load the field you need to put the entity back into an entitymangers context with em.merge(), then do the field access and then close the entitymanager.
You might want lazy loading when constructing a customer class with a collection for customer orders. If you retrieved every order for a customer when you wanted to get a customer list this may be a expensive database operation when you only looking for customer name and contact details. Best to leave the db access till later.
For the second part of the question - how to get hibernate to generate optimised SQL?
Hibernate should allow you to provide hints as to how to construct the most efficient query but I suspect there is something wrong with your table construction. Is the relationship established in the tables? Hibernate may have decided that a simple query will be quicker than a join especially if indexes etc are missing.
Try with:
select p from Person p left join FETCH p.address a where...
It works for me in a similar with JPA2/EclipseLink, but it seems this feature is present in JPA1 too:
If you use EclipseLink instead of Hibernate you can optimize your queries by "query hints". See this article from the Eclipse Wiki: EclipseLink/Examples/JPA/QueryOptimization.
There is a chapter about "Joined Reading".
to join you can do multiple things (using eclipselink)
in jpql you can do left join fetch
in named query you can specify query hint
in TypedQuery you can say something like
query.setHint("eclipselink.join-fetch", "e.projects.milestones");
there is also batch fetch hint
query.setHint("eclipselink.batch", "e.address");
see
http://java-persistence-performance.blogspot.com/2010/08/batch-fetching-optimizing-object-graph.html
I had exactly this problem with the exception that the Person class had a embedded key class.
My own solution was to join them in the query AND remove
#Fetch(FetchMode.JOIN)
My embedded id class:
#Embeddable
public class MessageRecipientId implements Serializable {
#ManyToOne(targetEntity = Message.class, fetch = FetchType.LAZY)
#JoinColumn(name="messageId")
private Message message;
private String governmentId;
public MessageRecipientId() {
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public String getGovernmentId() {
return governmentId;
}
public void setGovernmentId(String governmentId) {
this.governmentId = governmentId;
}
public MessageRecipientId(Message message, GovernmentId governmentId) {
this.message = message;
this.governmentId = governmentId.getValue();
}
}
Two things occur to me.
First, are you sure you mean ManyToOne for address? That means multiple people will have the same address. If it's edited for one of them, it'll be edited for all of them. Is that your intent? 99% of the time addresses are "private" (in the sense that they belong to only one person).
Secondly, do you have any other eager relationships on the Person entity? If I recall correctly, Hibernate can only handle one eager relationship on an entity but that is possibly outdated information.
I say that because your understanding of how this should work is essentially correct from where I'm sitting.

Categories

Resources