检查JPA是否存在行记录

时间:2018-11-24 15:24:23

标签: java jpa

我想检查按ID的行记录是否存在。我尝试过:

public Optional<PaymentTransactions> transactionByUnique_Id( String unique_id ) throws Exception
{
    String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.unique_id = :unique_id";
    TypedQuery<PaymentTransactions> query = entityManager.createQuery( hql, PaymentTransactions.class ) .setParameter( "unique_id", unique_id );
    Optional<PaymentTransactions> paymentTransaction = query.getResultList().stream().findFirst();
    return paymentTransaction;
}

Optional<PaymentTransactions> tnx = transactionsService.transactionByUnique_Id( transaction.getTransactionId() );

if(tnx.isPresent())
{
    return "test";
    // This method should be called only when DB record exists
}

但是我总是得到当前的对象。实施此检查的正确方法是什么?

0 个答案:

没有答案