I am using the following HQL query:
List<Object> object = session.createQuery("select userId, count(*) from Tweet " +
"group by userId", Object.class).getResultList();
It's causing the following error:
Caused by: org.hibernate.query.QueryTypeMismatchException: Query
result-type error - multiple selections: use Tuple or array at
org.hibernate.query.sqm.internal.QuerySqmImpl.checkQueryReturnType(QuerySqmImpl.java:367)
at
org.hibernate.query.sqm.internal.QuerySqmImpl.visitQueryReturnType(QuerySqmImpl.java:328)
at
org.hibernate.query.sqm.internal.QuerySqmImpl.(QuerySqmImpl.java:227)
at org.hibernate.internal.AbstractShare
what could be the reason for this?
Is it because I am selecting specific columns in it?
Your select clause is returning two things, therefore the type signature of the method should be List<Object[]>. Use this version:
List<Object[]> object = session.createQuery("select userId, count(*) from Tweet " +
"group by userId", Object[].class).getResultList();
But note that you could also define an entity which matches the select clause. Then, you could avoid the cumbersome Object[] result set type.
Related
Postgres query:
select t
from TableName t
where t.isActive=true
order by t.extcol->'desc'
This work's fine.
Need help with HQL query which will do the same job.
String query = "select t from TableName t where t.isActive=true order by t.extcol->'desc'"
newList = TableNameModel.executeQuery(query);
Method threw 'java.lang.IllegalArgumentException' exception.
context: there is a table TableName where one column is extcol which contains object. Need to sort TableName based on a property desc which present in extcol json object.
For HQL you have the wrong syntax for sorting.
It should be order by t.extcol desc without the arrow and quotes.
see https://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-ordering
I have query looks like SELECT * from TableName WHERE id= ?
for id field I want to pass array of ids which Number field in Database,
I have created Number Array type i database, when I pass Array to my prepared statement I get following error.
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * from Tablename WHERE id= ?]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected NUMBER got schema.app_id
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:95)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springfra
You need to use a table collection expression in your query:
The table_collection_expression lets you inform Oracle that the value of collection_expression should be treated as a table for purposes of query and DML operations. The collection_expression can be a subquery, a column, a function, or a collection constructor. Regardless of its form, it must return a collection value—that is, a value whose type is nested table or varray. This process of extracting the elements of a collection is called collection unnesting.
So you need to convert your passed collection (array) to a table in a subquery within your in() clause:
SELECT * from TableName WHERE in (SELECT column_value FROM TABLE(?))
Or with a join:
SELECT t.*
FROM TABLE(?) i
JOIN TableName t
ON t.id = i.column_value
I have defined this method
#Query("select cs from CenterStudy cs where cs.id in (?1)")
#EntityGraph(value = "centerStudyAndNotifiedUsers", type = EntityGraph.EntityGraphType.LOAD)
List<CenterStudy> findAllByIdsWithCenterAndUsers(List<Long> ids);
The list with the ids is not null, nor empty, but I always get the following exception:
java.lang.NullPointerException
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:613)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1900)
I'm pretty sure it has something to do with the way the IN clause it's defined.
Any suggestions are well welcomed!
It's a common issue, a bug in Hibernate which you can find at :
NullPointer when combining JPQL query with in clause and #NamedEntityGraph where it says that:
When you combine a JPQL query with an in clause and a #NamedEntityGraph
you get a NullPointerException in org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67) when you execute the query.
So you would have to remove the #EntityGraph annotation here in order to fix this problem.
public List<DealInfo> getDealInfos(List<String> dealIds) {
String queryStr = "SELECT NEW com.admin.entity.DealInfo(deal.url, deal.url, deal.url, deal.url, deal.price, deal.value) " + "FROM Deal AS deal where deal.id in :inclList";
TypedQuery<DealInfo> query = em.createQuery(queryStr, DealInfo.class);
query.setParameter("inclList", dealIds);
return query.getResultList();
}
Works with JPA 2
I've faced this issue before. I fixed it using named parameteres instead of positional parameters.
Example:
"Select p from product where p.id in(?)" -- not working
replaced by:
"Select p from product where p.id in(:idList)" -- OK
Updated
Error says:
ava.lang.String cannot be cast to com.test.test.classes.TblTaxType
what is happening is when I add the tag select distinct taxtcode error is appearing. But when I removed the select tag like FROM tblTaxType tbl_tax_type WHERE bfnsCode = ? everything is fine. What is the cause? this is my code:
String hql = "SELECT DISTINCT TAXT_CODE FROM tbl_tax_type WHERE BFNS_CODE = ?";
try {
setSession(HibernateUtil.getSession());
#SuppressWarnings("unchecked")
List <TblTaxType> resultList = getSession().createSQLQuery(hql)
.setString(0, bfnsCode)
.list();
Your entity is probably named TblTaxType, not tblTaxType. Case matters.
Side note: don't name sql an HQL query. SQL and HQL are different languages.
Solved it using GROUP BY instead by using DISTINCT.
String hql = "FROM TblTaxType tbl_tax_type WHERE bfnsCode = ? GROUP BY taxtCode";
Your query returns TAXT_CODE, this field is a property of your TblTaxType entity, so you can't cast one property (string) in your main entity. This is the reason of your error.
If you need complete entity you must change your query but DISTINCT is not useful in this case because if you extract complete entity, there's ID field (different for each row). If you want a first element, you can add in your query ORDER BY clause with LIMIT 1 (is MySql).
A solution with GROUP BY works only if you use MySql as DBMS because if you have Sql Server the correct behaviour of field list / group by is: a field in field list must be in GROUP BY cluse or must be in aggregate function.
OK, I have mapped with annotations two tables with a bidirectional #ManyToMany relationship.
Now I want to return only the elements which aren't in a many to many relationship, and I'm trying to use the code from here, but It throws an exception at runtime.
Here's the HQL:
String hql = "select a from Article a " +
"left join a.tags t " +
"group by a " +
"having count(t)=0";
Is there a better way to return those elements? Or to fix the error in this query?
The exception it throws now is:
column "article0_.id" must appear in the GROUP BY clause or be used in an aggregate function
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL ...
The query is correct, but on some databases group by a is not enough, you have to enumarate all properties of a, such as group by a.id, a.title.
Alternatively, you can use the following query:
select a from Article a where a.tags is empty
See also:
16.12. The group by clause
Not sure if it would work, but you can give it a try:
String hql = "select a from Article a " +
"where a.tags=null ";