I am starting to learn JPA, and have implemented an example with JPA query, based on the following native SQL that I tested in SQL Server:
SELECT f.StudentID, f.Name, f.Age, f.Class1, f.Class2
FROM Student f
LEFT OUTER JOIN ClassTbl s ON s.ClassID = f.Class1 OR s.ClassID = f.Class2
WHERE s.ClassName = 'abc'
From the above SQL I have constructed the following JPQL query:
SELECT f FROM Student f LEFT JOIN f.Class1 s;
As you can see, I still lack the condition OR s.ClassID = f.Class2 from my original query. My question is, how can I put it into my JPQL?
Write this;
SELECT f from Student f LEFT JOIN f.classTbls s WHERE s.ClassName = 'abc'
Because your Student entity has One To Many relationship with ClassTbl entity.
If you have entities A and B without any relation between them and there is strictly 0 or 1 B for each A, you could do:
select a, (select b from B b where b.joinProperty = a.joinProperty) from A a
This would give you an Object[]{a,b} for a single result or List<Object[]{a,b}> for multiple results.
Normally the ON clause comes from the mapping's join columns, but the JPA 2.1 draft allows for additional conditions in a new ON clause.
See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#ON
Please see :
public interface YourDBRepository extends JpaRepository<Employee, Long> {
#Query("select new com.mypackage.myDTO(dep.empCode, dep.empName, em.EmployeeCode, em.EmployeeName) \n" +
"from Department dep\n" +
"left join Employee em\n" +
"on dep.DepartmentCode = em.DepartmentCode") // this is JPQL so use classnames
List<myDTO> getDeptEmployeeList();
}
You can also use CrudRepository and include #JoinColumn with FK table class in PK table class and have List return list and then do find operation to achieve the same.
In Department entity class:
#OneToMany
#Fetch(FetchMode.JOIN)
#JoinColumn(name="DEPT_CODE")
private List<Employee> employees;
CriteriaBuilder is yet another option.
Related
I have written two hibernate queries:
TypedQuery q = em.createQuery("SELECT user.id FROM TableOne AS user WHERE ...", Long.class);
TypedQuery q = em.createQuery("SELECT link.user_id FROM TableTwo AS link WHERE ...", Long.class);
Now, how do I merge these two queries? My return type has to be TypedQuery
the UNION statement not work on Hibernate.
So you can:
Execute first query and put in a list;
Execute second query and put in a list;
Put the result of first and second list in a unique list.
If you want to delete the duplicated value, you must do programmatically.
Completely copying answer from https://stackoverflow.com/a/3940445/929701:
You could use id in (select id from ...) or id in (select id from ...)
e.g. instead of non-working
from Person p where p.name="Joe"
union
from Person p join p.children c where c.name="Joe"
you could do
from Person p
where p.id in (select p1.id from Person p1 where p1.name="Joe")
or p.id in (select p2.id from Person p2 join p2.children c where c.name="Joe");
At least using MySQL, you will run into performance problems with the later though. It's sometimes easier to do a poor man's join on two queries instead:
// use set for uniqueness
Set<Person> people = new HashSet<Person>((List<Person>) query1.list());
people.addAll((List<Person>) query2.list());
return new ArrayList<Person>(people);
It's often better to do two simple queries than one complex one.
I have a following code:
public class ValueDAO implements BusinessObject<Long> {
private Long id;
private String code;
private ClassDAO classDAO ;
....
}
public List<String> getCodesByCodeClass(Long classId) {
String select = "select distinct val.code from ValueDAO val left " +
"join fetch val.classDAO ";
String where = "where val.classDAO.id = ? order by val.code";
return getHibernateTemplate().find(select + where, classId);
}
It raises an exception:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
In the result I wan to get only codes.
join fetch val.classDAO.b means "when fetching val, also fetch the classDAO linked to the val". But your query doesn't fetch val. It fetches val.code only. So the fetch makes no sense. Just remove it, and everything will be fine:
select distinct val.code from ValueDAO val
left join val.classDAO classDAO
where classDAO.id = ?
order by val.code
Some notes, though:
doing a left join and then adding a retriction like classDAO.id = ? means that the join is in fact an inner join (since classDAO can't be null and have the given ID at the same time)
naming your entities XxxDAO is very confusing. DAOs and entities are not the same thing at all.
Given the above, the query can be rewritten as
select distinct val.code from ValueDAO val
where val.classDAO.id = ?
order by val.code
I want to do something like that with HQL:
SELECT *
FROM tableA a
INNER JOIN (select fieldA, sum(fieldB) as sum from tableB) b
ON a.fieldA = b.fieldA and a.fieldC = b.sum;
But this gives an error:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: (...
There is any way to make this using HQL and Hibernate?
try the native SQL solution approach:
need toimport this first:
import org.hibernate.SQLQuery;
then somewhere in your code:
SQLQuery query = session.createSQLQuery(
"SELECT * FROM tableA a
INNER JOIN
(SELECT fieldA, sum(fieldB) as sum from tableB) b
ON a.fieldA = b.fieldA and a.fieldC = b.sum"
);
more on this link
and HERE (
Joins in Hibernate Query Language)
You can try to do such thing with HQL:
String sqlText =
"select entityA
from EntityA entityA, EntityB entityB
where entityA.fieldA=entityB.fieldA
and entityA.fieldC=(select sum(entityB.fieldB)
from EntityB entityB
where entityB.fieldA=entityA.fieldA)"
Query query = session.createQuery(sqlText);
It should work similar to your sql. About your statement - as I know you cannot use inner view in HQL because it is object oriented.
Here is a good article about joins in HQL.
EDIT:
According to notes from user1495181 above query can be rewritten like (but I'm not sure):
String sqlText =
"select entityA
from EntityA entityA
join entityA.entitiesB entityB
Where entityA.fieldC=(select sum(entityB.fieldB)
from EntityB entityB
where entityB.fieldA=entityA.fieldA)"
But I prefer first variant because as for me it is more understandable (especially for peoples who used to work with native SQL).
You cannot define the join with on keyword. Hibernate know how to do the join based on your mapping.
If you define a relation in the mapping between a and b than hibernate will do the join based on the relation that you defined.
If you have relation between a and b than do inner join without using on and put the join criteria in the where clause
I need tune query for performance. The tree of objects like:
classA {
classB b;
classC c;
.....}
I need select similar to SQL:
select a.field1, b.field2, c.field3, c.field4 from a left outer join b
on a.id=b.fk left outer join c on b.id=c.fk
I don't understand what kind of result will be returned, does it arrayList? Or query returns
all 3 objects?
Thanks.
It will be
List<Object[]> list = new ArrayList<Object[]>();
The result which would be returned by the query would be type of -
List<Object[]>
If you use HQL I think you use hibernate. Provide mapping with relations (ManyToOne or OneToOne) to your objects:
class A {
#ManyToOne
pribvate B b;
#OneToOne
private C c;
}
Then use session methods to select your object A with hql query of criteria. Hibernate do all joins for you automatically. And it will return List of A.
I have the following HQL query which works fine, however it returns a list of full FooD objects. I only need the ID of the FooD objects as I need to have faster query. Please not that in Hibernate mappings, FooD has a many-to-one relationship with FooB.
hqlQuery = "from FooD d left join fetch d.bill where d.ts < :ts"
I have then tried to get only the ID using the same kind of HQL query:
hqlQuery = "SELECT d.id from FooD d left join fetch d.bill where d.ts < :ts"
I got a "query specified join fetching, but the owner of the fetched association was not present in the select list".
I have then converted the query to regular Oracle SQL to get only FooD.ID:
sqlQuery = "SELECT d.id from FooD d LEFT OUTER JOIN FooB b on d.foodId=b.id where d.ts < :ts"
I have then mapped FooD and FooB objects like this:
sqlQuery.addEntity(FooD.class);
sqlQuery.addEntity(FooB.class);
and then get the resulting list by calling:
hSession.createSQLQuery(sql).setTimestamp("ts", ts).list();
But got the following error: "unexpected token: on near line 1".
Does someone know how to do get only the ID of FooD when doing a left outer join on FooB using Hibernate?
Update:
I didn't test it, but this should do the trick
SELECT d.id from FooD d inner join d.bill where d.ts < :ts
when you add LEFT you make it an outer join implicitly, and there is no need to initialize bill if all you need is to join by keys
Hibernate requires the object to be in the select clause to do any eager join fetches on it
But since you have no select or where clauses on d.bill, why do you need to fetch it anyway?
If all you need is the id, why not do this, there is no reason for the redundant join:
hqlQuery = "SELECT d.id from FooD d where d.ts < :ts"