I'm getting the following exception when I try to run my HQL query:
java.util.concurrent.ExecutionException: javax.ejb.EJBException:
java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join
fetching, but the owner of the fetched association was not present in the select list
And here is the query I'm running:
SELECT new com.airit.propworks.dto.CompanyContactReportDTO(comp, compStatus.statusDesc)
FROM CoCompany as comp
LEFT JOIN FETCH comp.coCompanyCategoriesCompanyNumbers as compCat
LEFT JOIN FETCH comp.coContactCompanyNumbers as compCont
LEFT JOIN FETCH comp.coOperatingNamesCompanyNumbers
LEFT JOIN compCat.categoryFunctionCoCategoryList as compFcn
LEFT JOIN FETCH compCont.coContactDocumentssCompositeFK1 as contDoc
LEFT JOIN FETCH compCont.coContactJobssCompositeFK1 as contJob
LEFT JOIN FETCH compCont.coPhoneNumberssCompositeFK1
LEFT JOIN FETCH contDoc.documentTypeCoDocumentTypes as docTypes
LEFT JOIN FETCH contJob.contactFunctionCoContactFunctions as contFcn
LEFT JOIN comp.companyStatusCoCompanyStatuses as compStatus --this was added by me
WHERE comp.companyNumber = ? ORDER BY comp.companyName
The line second to the last was added by me and the constructor was added by me. It wasn't until I added those lines that I started getting the exception.
As you can see the second to last line referring to comp.companyStatusCoCompanyStatuses is a join that returns a single CoCompanyStatuses object that I then try to get the statusDesc string from and pass to the constructor.
I'm not sure what is causing the exception I'm getting. Do you guys see anything?
There's an opened bug for using select new combined with join fetch.
Since the problem is with the added select new, you can separate the retrieval from constructing CompanyContactReportDTO.
Related
Working with grails 3.3.8 and Mysql DB 5.5.x version.
I have done this query:
String query = $/
select new Map( i1.id as id,i1.name as name)
from CustomerComposition as c1
inner join c1.instrument as i1
left join i1.analisys as a1,
Instrument as i2
left join i2.analisys as a2,
Instrument as i3
left join i3.analisys as a3
where
i1.id=i2.id and i1.id=i3.id and
c1.portfolio.id=:ptfId and a1.phase.id=:p1Id and a2.phase.id=:p2Id and a3.phase.id=:p3Id
/$
List composition = CustomerComposition.executeQuery(query,
[ptfId: ptfId, p1Id: phase[0], p2Id: phase[1], p3Id: phase[2]])
The left join doesn't work. Then I realize that it doesn't work because I put a clause inside the WHERE. I double check with simple SQL statment and indeed it works once moved the conditional out the where. A simple snip taken just for clarification, before and it did not work:
SELECT
instrument1_.id
FROM
customer_ptf_composition customerpt0_
INNER JOIN instrument instrument1_ ON customerpt0_.instrument_id = instrument1_.id
LEFT JOIN analisys analisys4_ ON instrument1_.id = analisys4_.instrument_id
WHERE
customerpt0_.portfolio_id =1216
AND analisys4_.phase_id =111
and after and it works due to how left/right join works:
SELECT
instrument1_.id
FROM
customer_ptf_composition customerpt0_
INNER JOIN instrument instrument1_ ON customerpt0_.instrument_id = instrument1_.id
LEFT JOIN analisys analisys4_ ON instrument1_.id = analisys4_.instrument_id AND analisys4_.phase_id =111
WHERE
customerpt0_.portfolio_id =1216
Now my question is how can I put the "and field=value" next to the left join in GORM?
You can use the WITH clause to achive this:
left join i3.analisys a3 WITH a3.something = :someValue
See also: current Hibernate user guide: HQL explicit join
For the below left outer join, my Java application sometimes receives a fetch out of sequence error:
"JDBC error reported: (SQLState = HY010) - java.sql.SQLException: [tibcosoftwareinc][Oracle JDBC Driver][Oracle]ORA-01002: fetch out of sequence "
After cycling the Oracle server, the issue seems to go away for a little while, but is back only days later. For every 100 time this query runs, it might produce 20 fetch out of sequence errors. I haven't been able to reproduce it manual, like by running the query from SQL Developer.
I'm not sure what the issue is because the application isn't updating or inserting into these tables. The query runs only after the table is populated, and only one instance of the application runs, sequentially. I'm also not using any PL/SQL. Everything I read about this error results from select-into statements and misuse of cursors. I'm not using either.
Does anyone have an idea what might be wrong here?
Thanks
select r.report_key, r.total, r.custom11, r.custom12, r.C_DATE, r.ORG_UNIT3, sum(j.journal_amount), j.ACCOUNT_CODE,
j.PAYER_NAME, re.posted_amount, re.ENTRY_ID, re.custom39, re.custom40, av.POSTED_AMT, av.TAX_AMT,
e.FOREIGN_OR_DOMESTIC, em.EMP_ID, r.custom9, re.description
from report r
left outer join journal j on r.REPORT_KEY = j.REPORT_KEY and r.SEQUENCENUMBER = j.SEQUENCENUMBER
left outer join report_entry re on r.REPORT_KEY = re.REPORT_KEY and r.SEQUENCENUMBER = re.SEQUENCENUMBER
left outer join ENTRY_LOCATION e on r.REPORT_KEY = e.REPORT_KEY and r.SEQUENCENUMBER = e.SEQUENCENUMBER
left outer join EMPLOYEE em on r.REPORT_KEY = em.REPORT_KEY and r.SEQUENCENUMBER = em.SEQUENCENUMBER
left outer join VAT_TAX av on r.REPORT_KEY = av.REPORT_KEY and r.SEQUENCENUMBER = av.SEQUENCENUMBER
where r.batchid = ? and r.report_key = ? and j.ACCOUNT_CODE != 'CASHADV'
group by r.report_key, r.total, r.custom11, r.custom12, r.C_DATE, r.ORG_UNIT3, j.ACCOUNT_CODE,
j.PAYER_NAME, re.posted_amount, re.ENTRY_ID, re.custom39, re.custom40, av.POSTED_AMT, av.TAX_AMT,
e.FOREIGN_OR_DOMESTIC, em.EMP_ID, r.custom9, re.description
I'm trying to get the latest entry for each group and I got a query from this post
GROUP BY with MAX(DATE)
and here is the query
SELECT t1
FROM TrainTable t1 LEFT JOIN TrainTable t2
ON (t1.Train = t2.Train AND t1.Time < t2.Time)
WHERE t2.Time IS NULL;
When I try to implement the same query in Spring data JPA #Query, I'm getting
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT t1
FROM TrainTable t1 LEFT JOIN TrainTable t2
ON (t1.Train = t2.Train AND t1.Time < t2.Time)
WHERE t2.Time IS NULL]
Is there any possible way to use the same table entity in left join?
I have a pretty simple sql query:
SELECT o.description, oi.description, cu.name
FROM dbo.order o
LEFT JOIN dbo.orderitem oi o.orderItemId = oi.orderItemId
INNER JOIN dbo.customer cu on cu.customerId = o.customerId
WHERE cu.id = 12345
That query works, every time.
If I change the INNER JOIN to a LEFT JOIN...
LEFT JOIN dbo.customer cu on cu.customerId = o.customerId
then it always freezes when I call it with Java (via REST) and sometimes freezes if I am using my SQL Client. Sometimes it just works with the exact same search parameter and returns a few rows (<10) as expected.
Freezes means that it won't even start counting the execution time in my SQL client (SQuirreL) and that it will run into a timeout when I call it in Java.
But it will work again if I add a second WHERE clause like
SELECT o.description, oi.description, cu.name
FROM dbo.order o
LEFT JOIN dbo.orderitem oi o.orderItemId = oi.orderItemId
LEFT JOIN dbo.customer cu on cu.customerId = o.customerId
WHERE cu.id = 12345
AND o.someOtherId = 3456
or remove the cu.id completely.
Does anyone have any clue why that could happen?
I am not getting this line..
LEFT JOIN dbo.orderitem am oi o.orderItemId = oi.orderItemId
Dont you think it should be like:
LEFT JOIN dbo.orderitem oi on o.orderItemId = oi.orderItemId
Can you try?
This is because the left join on customer makes the where clause ineffective; the query would return all orders, with all orderitems, but only for orders of customer 12345, it would also return the customer name.
Maybe you want to do a right join instead, so you get the customer name even if he has no orders?
I have the below query in MySQL
SELECT Max(b.updated_at) as latest_date, r.resource,
COUNT(bill) as bills FROM Resource r
left join BillComp ba on (r.resource=ba.resource and comp=6)
This returns null values if t could not map a resource with the given company which is the requirement.
I need to convert to HQL and I am having issues in returning null raws as expected.
I have tried below two methods but none works
SELECT COUNT(bill), r.resource, MAX(b.updatedAt) FROM Resource r
left join r.billComp ba left join ba.bills b where ba.comp.comp = ?1
SELECT COUNT(bill), r.resource, MAX(b.updatedAt) FROM Resource r
left join r.billComp with ba.comp.comp = ?1 ba left join ba.bills b
Can someone show me how to get null values
I am using JPA Annotations