1

I have 4 different tables on which Joins are applied and this is working fine with SQL Query

SELECT donor.title, SUM(donation.donated_amount) + SUM(donation.gift_aid),    
SUM(donation.donated_amount)
FROM checkout checkout
JOIN donation donation ON donation.checkout_id = checkout.id
JOIN donor donor ON checkout.donor_id = donor.donor_id
JOIN company company ON company.id = donor.company_id
WHERE donation.donation_status = 1
GROUP BY donor.title;

Now I have to implment filters on the result set so I implemented this Criteria Builder Stuff.

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<DonorReportResponseDTO> criteriaQuery = criteriaBuilder.createQuery(DonorReportResponseDTO.class);
Root<Checkout> checkout = criteriaQuery.from(Checkout.class);
Join<Checkout, Donation> donation = checkout.join(Checkout_.donation);
Join<Checkout, Donor> donor = checkout.join(Checkout_.donor);
Join<Donor, Company> company = donor.join(Donor_.company);

But this is giving this error on first Join statement.

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null at org.hibernate.query.criteria.internal.path.AbstractFromImpl.constructJoin(AbstractFromImpl.java:293)
at org.hibernate.query.criteria.internal.path.AbstractFromImpl.join(AbstractFromImpl.java:279)
at com.netsol.fareshare.auth.repository.DonationRepositoryImpl.getDonorDetailReportByCriteria(DonationRepositoryImpl.java:35)
at com.netsol.fareshare.auth.repository.DonationRepositoryImpl$$FastClassBySpringCGLIB$$32b471a5.invoke(<generated>)
Faizan Haidar Khan
  • 1,099
  • 1
  • 15
  • 20
  • 1
    what is the error message or the complete stacktrace? as you commented under https://stackoverflow.com/a/42019970/6202869, maybe that answer is also helpful to understand this question? but you have to tell... – Roland Jun 13 '22 at 14:46
  • I have updated error message. It giving null pointer on first join statement. – Faizan Haidar Khan Jun 14 '22 at 05:29

1 Answers1

0

In the current scenario Metamodel was not picked by Join statement

Join<Checkout, Donation> donation = checkout.join(Checkout_.donation);

So I changed the Metamodel class instance to the variable I specified in Campaign Entity like this

Join<Checkout, Donation> donation = checkout.join("donation");

And changed it in all joins, group by and multiselect.

Faizan Haidar Khan
  • 1,099
  • 1
  • 15
  • 20