I would like to fetch data with one-to-many relation with inner join only.I can easily achieve the above using written direct HQL like this:
#Query("select distinct b from Batch b inner join fetch b.transactions")
But i dont want to write query as i am using JpaRepository interface which gives us very useful feature of Query By Method.
I also tried this.
#EntityGraph(attributePaths = { "transactions" } )
List<Batch> findDistinctByIdNotNull();
This avoids N+1 problem but performs left outer join between batch & transaction entity. Can any one help how to perform inner join with using Query method feature??
I'm trying to convert a native query into JPQL. In the native query there's a subquery in from clause. Something like below
select * from table1 join (select * from table2) on ...
Since I wasn't able to write the same in JPQL, as it doesn't allow subqueries in from clause, I have created another entity class which is annotated with hibernate #SubSelect("select * from table2"). Now I'm able to use this entity in the from clause of the above query in place of subquery.
However there are 2 things that are not quite right
#SubSelect is not working with JPQL query, so I have to put native query.
#SubSelect is a hibernate specific annotation and I'm trying to avoid hibernate specific packages.
Is there a #SubSelect equivalent for JPQL?
Any hint is appreciated.
I have two tables in two different schemas. I have to join these two tables and fetch the record in java. maria DB does not support functions, so i can't use functions. Anyone is having any thought on this ?
select t.field1, t2.field1
from schema1.tablename t,
schema2.tablename t2
where t.key = t2.key;
Is there any way to write criteria query on entities not having explicit joins ? By explicit join I mean that 2 tables in database have no foreign key relationship but some columns need to be fetched from both the tables so joins are required in query. I know that queries having join can be written with 'in' clause and criteria queries can be written with "In" criteria. I have written HQL for this case but please tell me how to write criteria query for this case.
Thanks in advance
In this case, the cross join would be solution, but that is possible ONLY with HQL. Check doc (small cite):
16.2. The from clause
Multiple classes can appear, resulting in a cartesian product or "cross" join.
from Formula, Parameter
from Formula as form, Parameter as param
And, also, we can filter on any of these two Entities inside of the WHERE clause, to narrow the cartesian product...
I have the following entities:
Machines {
id,
name
}
Favorites {
userId,
objectId,
objectType
}
Now I want to return list of machines ordered by favorites, name.
Favorites does not have any relation with Machines entities, its a generic entity which can hold various favoritable objects.
I got the sorting to work by the following raw sql. Is there any way to get this working using hibernate criterias. Basically ability to add alias for Favorites though Machines doesn't have any reference to it.
select m.* from Machines m left outer join Favorites f on m.id=f.objectId and f.userId =#userId order by f.userId desc, m.name asc
There are two ways to do this:
Use an SQL query or an SQL restriction for a criteria query.
Map the relation between Favorites and Machines as an Any type association.
Since you already have the query, executing it as a native SQL query through hibernate is the simplest solution:
sess
.createSQLQuery("SELECT ... ")
.addEntity(Machine.class)
.list();
The Any type association however, will probably benefit you in other situations where you want to query favoritables.
As said by #Maarten Winkels, you can use native SQL query that you have. You can do that but, if you want change your database then syntax may differs. So, it is recommended not to use native SQL query.
You cannot perform outer join using Hibernate criteria without any association between tables.
If you change your mind & want to add an association between these tables, then You can do something like below using criteria
List machines = session.createCriteria( Machines.class )
.createAlias("Machines", "m")
.createAlias("Favorites", "f", Criteria.LEFT_JOIN,
Restrictions.and(Restrictions.eq("m.id", "f.objectId"),
Restrictions.eq("f.userId", "#userId")))
.addOrder(Order.desc("f.userId"))
.addOrder(Order.asc("m.name"))
.list();
If for some reason you don't want to add any relationship between these tables, then you can use INNER JOIN or CROSS JOIN to get all machines with whatever criteria you want.
from Machines m, Favorites f where m.id = f.objectId and f.userId = #userId order by f.userId desc, m.name asc;
With inner joins there is one problem, if there is outer join relation in database then it doesn't work.
You can also write subqueries or separate queries & perform manual conditional checks between the results returned by those queries.
And one thing I want to ask you, What is the meaning of #userId in your SQL query ? I keep it as it is in my answer but, I didn't understand for what purpose # is there ?