Failed to join three entity models in Query annotation via Spring JPA Data - spring

I have three tables and mapping JPA entity models as followings in jsf 2.x application.
Foo Foo.java
Bar Bar.java
Zoo Zoo.java
The Foo has #oneToMany relationship to both Bar and Zoo in entity model context. In native sql, I was able to join three of them which worked fine.
select f.*, b.*, z.*
from Foo f
inner join Bar b
on f.foo_id = b.foo_id
inner join Zoo z
on z.foo_id = b.foo_id
where b.name = 'barName' and z.type = 'zooType";
I was trying to translate native sql in Query annotation via Spring JPA Data however I was keep getting org.hibernate.hgql.internal.ast.QuerySyntaxException: unexpected token.
Can someone kindly enough to point out what I am doing wrong? I tried having "one inner join" but I got same exception.
#Query("select f from Foo f inner join f.bars b inner join f.zoos z " +
"where b.name = ?1 " +
"where z.type = ?2")
List<Foo> findFoo(String name, String type);

This is because, you write two where in the #Query block, maybe you should use
#Query("select f from Foo f inner join f.bars b inner join f.zoos z " +
"where b.name = ?1 " +
"and z.type = ?2")
List<Foo> findFoo(String name, String type);
instead :)

Related

Select attribute of another attribute of entity

Currently I have three entities Country, Office and User, where Country.gov is of type Office and where Office.holder is of type User. Country is the owning side in Country.gov and Office is the owning side of Office.holder.
Now I want to get Country.gov with a LEFT JOIN on Office.holder using an attribute of Country, e.g.
SELECT c.gov o FROM Country c LEFT JOIN FETCH c.gov LEFT JOIN FETCH c.gov.holder WHERE c.countryKey = :countryKey, but this does not work, it throws an exception:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=dev.teamnight.game.entities.Country.gov,tableName=office,tableAlias=office1_,origin=country country0_,columns={country0_.gov_id,className=dev.teamnight.game.entities.Office}}]
[SELECT c.gov FROM dev.teamnight.game.entities.Country c LEFT JOIN FETCH c.gov LEFT JOIN FETCH c.gov.holder WHERE c.countryKey = :countryKey]
You can simplify a bit your query:
List<Office> calls = entityManager.createQuery(
"select o " +
"from Country c " +
"join c.gov o " +
"left join fetch o.holder " +
"where c.countryKey = :countryKey ", Office.class )
.setParameter( "countryKey", countryKey )
.getResultList();
For additional explanation, see this section of hibernate documentation.

Convert inner join native query to jpql

I have this method
#Query("select * from feed_tbl feed inner join view_tbl viewers on feed.id <> viewers.feed_id where viewers.user_id = :userId", nativeQuery = true)
fun findAll(#Param("userId") userId: Long): List<Feed>
entities:
User,
Feed
view_tbl is JoinTable in user entity
You should be able to use not in construct:
select f from Feed f where f not in (
select u.feeds from User u where u.id = :userId
)
Of cause you will need to map User to Feed as many-to-many

JPQL returning no results when one of the fields is null

I have a query which looks like this:
#Query(value = "SELECT e FROM Entity e") which returns all of the entities (currently 15). I want to only select certain fields to make the query far more memory efficient (has lots of embedded entities I don't need).
I have 2 many to one relationships, a and b, of which one is normally null and the other has a value. If I do #Query(value = "SELECT e.a FROM Entity e") I get the 10 results where a is not null, and when I do #Query(value = "SELECT e.b FROM Entity e") I get the other 5 where b is not null. However if I do #Query(value = "SELECT e.a, e.b FROM Entity e") I get 0 results back. I would assume it would all 15, returning it as a List<Object[]>. Why does this happen, and can I change it so #Query(value = "SELECT e.a, e.b FROM Entity e") returns all of the entities?
If e.a and e.b are entities you need left join
select a, b from Entity e left join e.a a left join e.b b

Join using two different entities in spring data jpa

I have 2 different entities. table1 has a column which is primary key of table2.
both of the tables has respected repository.
If I write below query in one of the repository it gives error
QuerySyntaxException: unexpected token:
#Query("select new stats.UserCountDTO(b.objectiveId, count(b.objectiveId), a.locationCountry)"+
" from UserIdentityEntity a, UserObjectiveEntity b where b.userIdentityId == a.id and b.cId = ?1")
How can I write join queries in this case using spring data jpa?
You just make a mistake. The
b.userIdentityId == a.id
should be:
b.userIdentityId = a.id

HQL map with distinct not work

I have a trouble using HQL (in Groovy):
A simple query works without problems, like below (please note that I am using distinct):
def eqLiquid = liq.Liquidacion.executeQuery(
"""SELECT
distinct b.id ,
l.nombre as name
FROM Liquidacion l JOIN l.detalles ll JOIN ll.bioquimico b
WHERE l.id=:liqid
""", [liqid: liqid])
But I need to return these data as a map, then I have modified the query (only add new map()) :
def eqLiquid = liq.Liquidacion.executeQuery(
"""SELECT new map(
distinct b.id ,
l.nombre as name
)
FROM Liquidacion l JOIN l.detalles ll JOIN ll.bioquimico b
WHERE l.id=:liqid
""", [liqid: liqid])
Then I get an error: "unexpected token: distinct near line 2, column 17 [SELECT new map(
distinct b.id ,"
If I quit the distinct in the last query, it works.
Anyone had this problem?
It seems that MAP and DISTINCT can not work togheter in an HQL query

Resources