JayCold
JayCold

Reputation: 47

jpa createQuery select column

I want to make query for specific column

I tried this. and this one worked.

List<ProductInfo> productInfo = em.createQuery( "select m from ProductInfo AS m", ProductInfo.class).getResultList();           

But below were not working.

List<ProductInfo> productInfo = em.createQuery( "select m.model from ProductInfo AS m", ProductInfo.class).getResultList();     

//or
List<ProductInfo> productInfo = em.createQuery( "select model from ProductInfo AS m", ProductInfo.class).getResultList();       

Upvotes: 0

Views: 661

Answers (1)

Abhinav Manthri
Abhinav Manthri

Reputation: 348

Since your query is specific to a column, it will return a list of Object, which contains all your column results

List<Object> productInfo = em.createQuery( "select m.model from ProductInfo AS m", Object.class).getResultList();     

or to retrieve results as list of ProductInfo

TypedQuery<ProductInfo> typedQuery = em.createQuery("SELECT NEW <package-name>.ProductInfo(m.model) from ProductInfo AS m", ProductInfo.class);    
List<ProductInfo> results = typedQuery.getResultList();

Upvotes: 1

Related Questions