1

I want to implement JPA query with INNER JOIN. I tried this:

@Override
    public Optional<PaymentTransactions> paymentTransactionByWpfPaymentId(Integer id) {
        String hql = "SELECT t.* FROM " + PaymentTransactions.class.getName() + " t " 
                + " INNER JOIN " + WpfPaymentPaymentTransactions.class.getName() + " wppt "
                + " ON t.id = wppt.payment_transaction_id " 
                + " WHERE wppt.wpf_payment_id = :id "
                + " ORDER BY t.id ASC LIMIT 1";
        TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class).setParameter("id", id);
        List<PaymentTransactions> wpfPayments = query.getResultList();
        return wpfPayments.isEmpty() ? Optional.empty() : Optional.of(wpfPayments.get(0));
    }

But I get this error when I run the code:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting IDENT, found '*' near line 1, column 10 [SELECT t.* FROM org.datalis.plugin.entity.PaymentTransactions t  INNER JOIN org.datalis.plugin.entity.WpfPaymentPaymentTransactions wppt  ON t.id = wppt.payment_transaction_id  WHERE wppt.wpf_payment_id = :id  ORDER BY t.id ASC LIMIT 1]

Do you know how I can fix this issue and what's causing it?

Probably I need to implement Spring Repository and make native query?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

3

Replace t.* with t.

You want PaymentTransactions object to be returned, not values of its fields.

Also HQL is not SQL, it doesn't support *.

talex
  • 17,973
  • 3
  • 29
  • 66
  • How I get `message: "org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: LIMIT near line 1, column 227 [SELECT t FROM org.datalis.plugin.entity.PaymentTransactions t INNER JOIN org.datalis.plugin.entity.WpfPaymentPaymentTransactions wppt ON t.id = wppt.payment_transaction_id WHERE wppt.wpf_payment_id = :id ORDER BY t.id ASC LIMIT 1]"` – Peter Penzov Mar 21 '19 at 11:06
  • @PeterPenzov : see https://stackoverflow.com/questions/1239723/how-do-you-do-a-limit-query-in-hql – Arnaud Mar 21 '19 at 11:13