Converting HQL to Criteria - java

HQL I want to convert in the Criteria.
HQL
select com from News as news " +
"join news.comments as com " +
"where news.id = :id " +
"order by com.addDate desc"
Criteria
DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
criteria.add(Restrictions.idEq(id));
DetachedCriteria cComment = criteria.createCriteria("comments");
cComment.addOrder(Order.desc("addDate"));
List<Comment> list = (List<Comment>)findByCriteria(cComment, false);
Criteria is working, but returned list News. I want to return the Сomments. Please help me?

try this may help you:
DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
criteria.setProjection( Projections.property("com"), "com"));
criteria.add(Restrictions.idEq(id));
DetachedCriteria cComment = criteria.createCriteria("comments");
cComment.addOrder(Order.desc("addDate"));
List<String> list = (List<String>)findByCriteria(cComment, false);

Related

Get values using good Hibernate practices

Good afternoon people!
I think that may be a silly question ..
I have the following code in Hibernate:
query = session.createQuery ("select F from Employee F where F.email =" + email);
Does anyone know how I can get the value of a field within this query?
Example: How would I get the name of the person (employee).
Note: I would like to use a good Hibernate practice ... I believe it is not good to repeat:
query = session.createQuery ("select F.person from Employee F where F.email =" + email);
Can you help me? :)
Thank you.
Prior to Hibernate 5.2:
String sql = "SELECT e.person FROM Employee e WHERE e.email = :email";
Query query = session.createQuery( sql )
query.setParameter( "email", emailAddress );
List<Person> people = (List<Person>) query.getResultList();
In Hibernate 5.2 and beyond:
String sql = "SELECT e.person FROM Employee e WhERE e.email = :email";
Query query = session.createQuery( sql, Person.class );
query.setParameter( "email", emailAddress );
List<Person> people = query.getResultList();
With the merge of Hibernate EntityManager into Core as a part of 5.2.x+, you now get better type safe queries to avoid casting later on :).

hql in-clause with multiple hits in child collection

I have a table/entity called Recipe with a child collection of type Tag.
I want to be able to find a Recipe searching by two or more tags. Something like:
SELECT re FROM Recipe re JOIN re.tags t WHERE t in :tagsIds
but I only want those hits where the Tag collection contains all tagIds.
Is it possible in HQL/SQL? (Maybe using Criteria?)
Thanks in advance.
I am assuming you have two different entities Recipe and Tag, it can be done as below.
Criteria criteria = getSession().createCriteria("Recipe.class");
criteria.createAlias("tags", "tag");
criteria.add(Restrictions.in("tag.id", Arrays.asList(1,2,3)));
return (List<Recipe>) criteria.list();
I believe you may be missing parentheses in the HQL. It should be:
... WHERE t in (:tagsIds)
Ok, so this did it. Thanks for the replies.
String hql = "select r from Recipe r " +
"join r.tags t " +
"where t.id in (:tags) " +
"group by r " +
"having count(t)=:tag_count";
Query query = session.createQuery(hql);
query.setParameterList("tags", tagIds);
query.setInteger("tag_count", tagIds.size());
List<Recipe> recipes = query.list();

Writing sql query in hibernate

I have a sql query:
select COUNT (distinct agentG) as count from Test_CPView where kNum = ‘test k1’ and pName = ‘test p1’
I'm trying to write into criteria query but it hasn't worked for me:
statelessSession = sessionFactory.openStatelessSession();
Criteria crit = statelessSession.createCriteria(APRecord.class, "apr");
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("pName"));
projList.add(Projections.groupProperty("kNum"));
projList.add(Projections.countDistinct("agentG"));
crit.setProjection(projList);
This produces:
Hibernate: select this_.pName as y0_, this_.kNum as y1_, count(distinct this_.agentG) as y2_ from Test_CPView this_ where (lower(this_. pName + '~' + this_. kNum) like ? or lower(this_. pName + '~' + this_. kNum) like ? or lower(this_. pName + '~' + this_. kNum) like ? or lower(this_. pName + '~' + this_. kNum) like ?) group by this_.pName, this_. kNum
and the return results are null.
How can I convert the above sql query into hibernate?
Session.createCriteria from Docs
You have not added Restrictions
statelessSession = sessionFactory.openStatelessSession();
Criteria crit = statelessSession.createCriteria(APRecord.class, "apr");
crit .add(Restrictions.eq("kNum", "test k1"));
crit .add(Restrictions.eq("pName ", "test k1"));
crit.setProjection(Projections.countDistinct("agentG"));
Integer count = crit.uniqueResult();
statelessSession = sessionFactory.openStatelessSession();
Criteria crit = statelessSession.createCriteria(APRecord.class, "apr");
crit.add(Expression.eq("kNum","test k1"));
crit.add(Expression.eq("pName","test p1"));
crit.setProjection(Projections.countDistinct("agentG"));
Query query = session.createQuery("count(agentG) from APRecord where kNum = :kNum and pName = :pName");
query.setParameter("kNum", "test k1");
query.setParameter("pName", "test p1");
return (Integer) query.uniqueResult();

Is there any spring jpa equivalent to following query

Query :
#Query("Select p.name,t.points from Player p,Tournament t where t.id=?1 And p.id=t.player_id")
I have my player and tournament entity and their corresponding JPA repositories. But the problem is we can get only entities from our query, but i want to do above query, please help me with this i am new to it.
this is my sql query i want to add but where to add i am not getting:
Select p.name, t.points_rewarded from player p, participant t where t.tournament_id="1" and t.player_id=p.id;
This is how you can do it with JPQL for JPA:
String queryString = "select p.name, t.points from Tournament t," +
" Player p where t.player_id=p.id " +
"and t.id= :id_tournament";
Query query = this.entityManager.createQuery(queryString);
query.setParameter("id_tournament", 1);
List results = query.getResultList();
You can take a look at this JPA Query Structure (JPQL / Criteria) for further information about JPQL queries.
And this is ho you can do it using HQL for Hibernate, these are two ways of doing it:
String hql = "SELECT p.name, t.points from Player p,Tournament t WHERE t.id= '1' And p.id=t.player_id";
Query query = session.createQuery(hql);
List results = query.list();
Or using query.setParameter() method like this:
String hql = "SELECT p.name, t.points from Player p,Tournament t WHERE t.id= :tournament_id And p.id=t.player_id";
Query query = session.createQuery(hql);
query.setParameter("tournament_id",1);
List results = query.list();
You can take a look at this HQL Tutorial for further information about HQL queries.
Note:
In both cases you will get a list of Object's array List<Object[]> where element one array[0] is the p.name and the second one is t.points.
TypedQuery instead of normal Query in JPA
this is what i was looking for, thanks chsdk for help, i have to create pojos class, and in above link answer is working fine foe me,
Here is my code sample
String querystring = "SELECT new example.restDTO.ResultDTO(p.name,t.pointsRewarded) FROM Player p, Participant t where t.tournamentId=?1 AND t.playerId = p.id ORDER by t.pointsRewarded DESC";
EntityManager em = this.emf.createEntityManager();
try {
Query queryresults = em.createQuery(querystring).setParameter(1, tournamentId);
List<ResultDTO> result =queryresults.getResultList();
return new ResponseEntity<>(result, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} finally {
if (em != null) {
em.close();
}}

Spring hibernate template list as a parameter

i'm trying to execute this query :
Code:
this.getHibernateTemplate()
find("select distinct ci.customer " +
"from CustomerInvoice ci " +
"where ci.id in (?) " , ids);
with ids as a List, id is of type Long
when executing i get exception
Code:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.param.PositionalParameterSpecification.bind(PositionalParameterSpecification.java:39)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:849)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:840)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:836)
at
If you want to add a list to an in clause it is best to use a named parameter. This is done like so.
Query q = this.getHibernateTemplate().getSession().createQuery("select distinct ci.customer " +
"from CustomerInvoice ci " +
"where ci.id in (:idsParam) ");
q.setParameter("idsParam", ids);
List<Customer> = q.getResultList();
In addition to mR_fr0g's answer, this one also works:
this.getHibernateTemplate()
findByNamedParam("select distinct ci.customer " +
"from CustomerInvoice ci " +
"where ci.id in (:ids) ", "ids", ids);
You could use the Hibernate Criteria API, it has a so called "in" Restriction.
Reference:
Restrictions.in(String, Collection)
Btw. be aware of cases where the ids collection is empty! (not only if you use the criteria API)
You can use parameter list to inlcude in your query with 'IN' and 'setParameterList'
List<Long> ids= new ArrayList<Long>();
Query query = getSession().createQuery("select distinct ci.customer from CustomerInvoice ci where ci.id in (:ids) ");
query.setParameterList("ids", ids);
query.executeUpdate();
ProjectionList projList =
Projections.projectionList().add("customer","customer");
List<Long> ids = ids;
Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession()
.createCriteria(CustomerInvoice.class)
.add(Restrictions.in("id",ids))
.setProjection(projList); List<Long> listOfIds = criteria.list();

Categories