In this query:
String sql="select BookCategory category from LibraryBook,BookMaster,BookCategory where (LibraryBook.id="+bookid+" AND LibraryBook.BookMaster.id=BookMaster.id AND BookMaster.BookCategory.id=BookCategory.id)";
I'm getting error:
unexpected token: category near line 1, column 22 [select BookCategory category from com.xtr.schoolmanager.domain.facility.library.LibraryBook ,com.xtr.schoolmanager.domain.facility.library.BookMaster,com.xtr.schoolmanager.domain.facility.library.BookCategory where (LibraryBook.id=18 AND LibraryBook.BookMaster.id=BookMaster.id AND BookMaster.BookCategory.id=BookCategory.id)]; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: category near line 1, column 22 [select BookCategory category from com.xtr.schoolmanager.domain.facility.library.LibraryBook ,com.xtr.schoolmanager.domain.facility.library.BookMaster,com.xtr.schoolmanager.domain.facility.library.BookCategory where (LibraryBook.id=18 AND LibraryBook.BookMaster.id=BookMaster.id AND BookMaster.BookCategory.id=BookCategory.id)]
Please help me to get a correct answer for this.
This is the problem:
select BookCategory category
I'm guessing you want to select the category column from BookCategory table? In which case it should read:
select BookCategory.category
Related
I am getting error in below query as
#Query(value = "SELECT top 1 * from Job where status = 'Success' order by 1 desc")
public Job getLastJob();
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: top near line 1,
I want to select 1st row which has success status order by id desc
I trying use querydsl query with having clause with count aggregation but i have the error
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: having near line 4, column 1 [select count(distinct logData.ipAddress)from com.ef.domain.LogData logData where logData.date between ?1 and ?1 having count(logData) >= ?2]
By the following code
return query.select(Projections.constructor(LogData.class, data.count(), data.ipAddress)).from(data).where(data.date.between(args.getStartDate(), args.getFinalDate()))
.groupBy(data.ipAddress).having(data.count().goe(args.getThreshold())).fetchResults().getResults();
What´s wrong?
I want to display all the checklists that are not answered and not answered (response checklists is in the ResponsesCheckLists table) using the following parameters: idequipement and idMission.
#Query("SELECT check,resp,eq FROM Equipements eq INNER JOIN CheckLists check WHERE eq.idEquipements = check.equipements.idEquipements LEFT JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss WHERE eq.idEquipements = :idEqp ")
public List<ResponsesCheckLists> ListCheckListsNonRepondu(#Param("idEqp") long idEqp, #Param("idmiss") long idmiss);
After running this query, I displays this error message:
antlr.NoViableAltException: unexpected token: LEFT
------
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: LEFT near line 1, column 148 [SELECT check,resp,eq FROM com.SSC.DAO.Entities.Equipements eq INNER JOIN CheckLists check WHERE eq.idEquipements = check.equipements.idEquipements LEFT JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss WHERE eq.idEquipements = :idEqp ]
------
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'responsesCheckListsRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.SSC.DAO.JPARepository.ResponsesCheckListsRepository.ListCheckListsNonRepondu(long,long)!
Edit1:
#Query("SELECT check,resp,eq FROM Equipements eq INNER JOIN CheckLists check ON eq.idEquipements = check.equipements.idEquipements"
+ " INNER JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss AND eq.idEquipements = :idEqp ")
public List<ResponsesCheckLists> ListCheckListsNonRepondu(#Param("idEqp") long idEqp, #Param("idmiss") long idmiss);
Error of Edit1;
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'check' {originalText=check}
antlr.SemanticException: Path expected for join!
Caused by: java.lang.IllegalArgumentException: Validation failed for
query for method public abstract java.util.List
com.SSC.DAO.JPARepository.ResponsesCheckListsRepository.ListCheckListsNonRepondu(long,long)!
Edit2:
#Query("SELECT check , resp , eq FROM Equipements eq INNER JOIN CheckLists check ON eq.idEquipements = check.equipements.idEquipements"
+ " INNER JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss AND eq.idEquipements = :idEqp ")
public List<ResponsesCheckLists> ListCheckListsNonRepondu(#Param("idEqp") long idEqp, #Param("idmiss") long idmiss);
Errors of Edit2:
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'check' {originalText=check}
antlr.SemanticException: Path expected for join!
How to correct this query?
thank you in advance
You have a Spring annotation there (#Query) that specifies a JPQL query. Your JPQL query is supposed to follow the syntax highlighted in this link (and the JPA spec). Sadly you haven't followed that.
SELECT {result} FROM {from} WHERE {where} ...
Any "JOIN" has to go in the FROM clause. You already put one JOIN in the FROM clause, but for reasons only known to you, you decided to put another JOIN in the WHERE clause!! In fact you have 2 WHERE clauses in that crap.
It is impossible to tell you what your query should be because you don't post your entities, so we don't see what relations they have, or even what you are trying to achieve. We can only point out your error
I have the following Spring Data Query:
#Query(value = "select * from person where person_id =?! and family_id not in (select related_person_id from relationships where related_family_id = ?1)", native query = true)
Person getPerson(String personId);
I am getting the error:
Caused by: java.sql.SQLException: Invalid column name
However, I know that all my column names for the two tables in my query are correct, what could ne causing this?
i don't know the structure of your data but your spring data query has many typos and errors, the standard query method should be:
#Query(value = "select * from person where person_id =?1 and family_id not in (select related_person_id from relationships where related_family_id = ?2)", nativeQuery = true);
Person findByPersonIdAndRelatedFamilyId(String personId, String relatedFamilyId);
also check your inner select query -I don't know the relation between family_id and related_person_id- but it should return a family_id column or an aliased column as family_id may be thats why you're receiving such error ..
We are trying to create hibernate specific query for below
lSession.createQuery(lQueryString.toString());
............
INSERT INTO EMPLOYEE_HISTORY (emp_status,
createdBy,
releasedate,
year)
SELECT CASE n.emp_Status WHEN 0 THEN 'Draft' ELSE 'Final' END,
m.createdBy,
m.releasedate,
m.year
FROM EMPLOYEE m, (SELECT COUNT (1) EMPLOYEE_HISTORY b,EMPLOYEE a where a.emplid=b.emplid and a.year=b.year and b.year=2012 and a.doc_id='XYZ') n
where a.doc_id='xyz'
getting following exception,I guess it because of below statement in query
(SELECT COUNT (1) EMPLOYEE_HISTORY b,EMPLOYEE a where a.emplid=b.emplid and a.year=b.year and b.year=2012 and a.doc_id='XYZ') n
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 374
INSERT INTO EMPLOYEE_HISTORY (emp_status,
createdBy,
releasedate,
year)
SELECT CASE n.emp_Status WHEN 0 THEN 'Draft' ELSE 'Final' END,
m.createdBy,
m.releasedate,
m.year
FROM EMPLOYEE m, (SELECT COUNT (1) EMPLOYEE_HISTORY b,EMPLOYEE a where a.emplid=b.emplid and a.year=b.year and b.year=2012 and a.doc_id='XYZ') n
where a.doc_id='xyz'
The Exception
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
Please provide if any suggestion/Inputs.
It looks, like you pass SQL instead of HQL to the Session.createQuery(...) method.
See: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html#objectstate-querying-executing
For example in:
INSERT INTO EMPLOYEE_HISTORY (...)
'EMPLOYEE_HISTORY' should be entity class name (like EmployeeHistory) not the table name.
Also in HQL you can use references defined in your entities to go through associations avoiding implicit joining by ids, like you do here
where a.emplid=b.emplid and ...
Consider example from URL above:
select mother from Cat as cat join cat.mother as mother where cat.name = ?