简体   繁体   中英

Hibernate native query shows exception

I have written hibernate native query for getting total users and month from table.
But it shows :

exception:Operand should contain 1 column(s)
org.hibernate.exception.DataException: could not execute query

could anyone please help me for solving this:

  c.setProjection(Projections.projectionList()
     .add(Projections.sqlProjection("(select count(*), MONTH(Stored) from adbtbl_stat " 
     + "where Activity = 89 and YEAR(Stored) = 2015) as Count, month",
     new String[] { "Count", "month" }, new Type[] { LongType.INSTANCE, StringType.INSTANCE }))
     .add(Projections.groupProperty(month(stored))));

A ProjectionList can contain multiple SQLProjection(s) , but each SQLProjection should be associated to one and only one SQL columns.

You SQLProjection tries to select two columns instead.

Try replacing your Criteria query with a simple native query instead:

Long activity = ...;
int year = ...;

Query q = entityManager.createNativeQuery(
    "select count(*) as Count, MONTH(Stored) as Month " +
    "from adbtbl_stat " +
    "where Activity = :activity and YEAR(Stored) = :year"
);
q.setParameter("activity", activity);
q.setParameter("year", year);
List<Object[]> result = (List<Object[]>) q.getResultList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM