Page 1 of 1

How to query an extended entity

Posted: Fri Jan 17, 2014 7:37 am
by Theo Schumacher
Hello,

I have extended ProdutImpl to SeaProductImpl and added two String fields. Admin and Site work well with this extension.

I need now to query the Product table on those two fields.

I tried to extend ProductDaoImpl into SeaProductDaoImpl and add a new query method like this but already the line in bold fails. Can you give any hint how to do correctly ?


public Product readProductByCompuzzId(String compuzzId, String cAccount) {
if (compuzzId == null || "".equals(compuzzId) || cAccount==null || "".equals(cAccount)) {
return null;
}
TypedQuery<SeaProductImpl> query = em.createNamedQuery("SEA_READ_PRODUCTS_BY_NAME", SeaProductImpl.class);
query.setParameter("compuzzProduct_UID", compuzzId);
query.setParameter("compuzzCaccount_UID", cAccount);
query.setHint(QueryHints.HINT_CACHEABLE, true);
query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
List<SeaProductImpl> resultList=null;
try {
resultList = query.getResultList();
} catch (Exception e) {
System.out.println("exception reading SeaProductImpl");
}
if (resultList == null) {
System.out.println("result list is empty");
return null;
} else {
return query.getResultList().get(0);
}

return null;




}
}


Thank you for any help.

Re: How to query an extended entity

Posted: Fri Jan 17, 2014 10:27 am
by Theo Schumacher
Hi,

solved my problem. Here is a code snippet that works for me:

// Set up the criteria query that specifies we want to return SeaProductImpl
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<SeaProductImpl> criteria = builder.createQuery(SeaProductImpl.class);

// The root of our search is SeaProductImpl since we are searching
Root<SeaProductImpl> product = criteria.from(SeaProductImpl.class);

// Product objects are what we want back
criteria.select(product);

// We only want results that match the search query, compuzzId and cAccount are Strings
List<Predicate> restrictions = new ArrayList<Predicate>();
restrictions.add(
builder.and(
builder.equal(product.get("compuzzProduct_UID"), compuzzId),
builder.equal(product.get("compuzzCaccount_UID"), cAccount)
)
);
// Execute the query with the restrictions
criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

TypedQuery<SeaProductImpl> typedQuery = em.createQuery(criteria);
//don't cache - not really practical for open ended search

List<SeaProductImpl> resultList = typedQuery.getResultList();
if (resultList.size()==0) {
return null;
} else {
return resultList.get(0); // only interested in the first result here
}

Re: How to query an extended entity

Posted: Thu Jan 23, 2014 1:58 pm
by RapidTransit
Just a quick question were you having a problem with the TypedQuery not finding your *.orm.xml file?

Re: How to query an extended entity

Posted: Mon Jan 27, 2014 4:20 am
by Theo Schumacher
Problem was 'typed Query not found'