java - JPA alternative of Hibernate Projections.property -


folks!

trying limit amount of columns fetched db , found this: hibernate criteria query specific columns

criteria cr = session.createcriteria(user.class)     .setprojection(projections.projectionlist()       .add(projections.property("id"), "id")       .add(projections.property("name"), "name"))     .setresulttransformer(transformers.aliastobean(user.class));    list<user> list = cr.list(); 

this works awesome when use hibernate, trying same jpa (jpa provider hibernate tho). is possible jpa? mean limit columns criteriabuilder , map specific object?

also, saw this: https://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/chapters/query/criteria/criteria.html

hibernate offers older, legacy org.hibernate.criteria api should considered deprecated. no feature development target apis. eventually, hibernate-specific criteria features ported extensions jpa javax.persistence.criteria.criteriaquery. details on org.hibernate.criteria api, see legacy hibernate criteria queries. 

so seems possible via hibernate extensions jpa?

there no alternative syntax within jpa, hibernate superset of jpa, provides features beyond jpa specifications. in jpa have projections individually explicitly select statement (in case multiselect statement).

criteria query

 // create instance of criteriabuilder, em entitymanager criteriabuilder cb = em.getcriteriabuilder();          // use criteriabuilder interface create instance    // of criteriaquery. multiselect result set object []. criteriaquery<object[]> c = cb.createquery(object[].class);     // establish root of query invoking from() root object.  root<user> user = c.from(user.class);     // establish select clause of query passing root multiselect() method criteriaquery.multiselect(user.get("property1"), user.get("property2")); 

alternative constructor call:

criteriaquery<user> c = cb.createquery(user.class); root<user> user = c.from(user.class); c.select(cb.construct(user.class,                       user.get("property1"),                       user.get("property2"))); 

tuplequery

    criteriaquery<tuple> c = cb.createtuplequery();     root<user> user = c.from(user.class);     c.select(cb.tuple(user.get("property1"), user.get("property2")));     query query = entitymanager.createquery(c);     list<tuple> results = query.getresultlist(); 

there array() method (it returns object[]) well.

    c.select(cb.array(user.get("property1"), user.get("property2"))); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -