0

I have an entity orderdetails, where a user can have many ordernames I want to get all the ordername by userid using jpa named query. I tried this

SELECT o.orderName FROM OrderDetails o WHERE o.userId=:userId; 

Since the return type will be List in the resultset, I executed the query like this

getEntityManager().createNamedQuery("getOrderNamesByUserId", 
orderDetail.class).setParameter("userId", userId);

This obviously is not working. How can I get that query working? One way is to iterate the List but I wonder whether there is another way around?

Jacob
  • 14,463
  • 65
  • 207
  • 320
Maverick
  • 1
  • 3

2 Answers2

0

Well this is non-compiled code snippet, you could try the following approach.

@Override
public List<OrderDetails> findOrders(Long userId) {
        TypedQuery<OrderDetails> query = entityManager.createNamedQuery(
                "OrderDetails.getOrderNamesByUserId", OrderDetails.class);
        query.setParameter("userId", userId);
        List<OrderDetails> list = query.getResultList();
        return list;
    }
Jacob
  • 14,463
  • 65
  • 207
  • 320
0

If you just need to select one column(ordername), use getResultList() method.

    Query query = getEntityManager().createNamedQuery("getOrderNamesByUserId", OrderDetail.class);
    query.setParameter("userId", userId);
    List<String> orderNameList = query.getResultList();
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131