I am trying to retrieve data from my database with hibernate but it keeps throwing an exception
2012-11-11 11:35:45,943 [main] ERROR
com.storage.hibernate.DatabaseAccessRequestsImpl - there was an error
javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not execute query
#Override
public List<Trade> requestPeriod() {
List<Trade> trades = null;
EntityManager manager = emf.createEntityManager();
Query query = manager.createQuery("from trade");
try{
trades = query.getResultList();
}
catch(PersistenceException e){
logger.error("there was an error " + e);
}
catch(SQLGrammarException e){
logger.error("there was an error " + e);
}
return trades;
}
I am guessing the syntax I am using for select all is incorrect but after looking around I can not see an alternative?
Thanks
It should be "from Trade" (uppercase T) as Trade is the name of the mapped class.
Note that in JPA QL SELECT clause is mandatory, as per: 10.2.1.1. JPQL Select Statement:
A select statement is a string which consists of the following clauses:
a SELECT clause, which determines the type of the objects or values to be selected;
a FROM clause, which provides declarations that designate the domain to which the expressions specified in the other clauses of the query apply;
[...]
In BNF syntax, a select statement is defined as:
select_statement ::= select_clause from_clause [where_clause] [groupby_clause] [having_clause] [orderby_clause]
The bare from Trade syntax is Hibernate-specific, to be specification compliant you should always use:
select t from Trade t
Related
Im using Spring mvc, Hibernate and Mysql, in my DAOImplement i call sql file outsite.
This is my DAO:
public List<RenterDto> searchByNamePersonalId(String name, String personalId) {
Session session = sessionFactory.openSession();
String sqlString = GetSqlUtils.getSqlQueryString(RenterDaoImpl.class, SQL_DIR + SEARCH_BY_NAME_PERSON);
List<RenterDto> list = new ArrayList<RenterDto>(0);
try {
Query query = session.createSQLQuery(sqlString);
query.setParameter(0, name);
query.setParameter(1, personalId);
list = query.setResultTransformer(new AliasToBeanResultTransformer(RenterDto.class)).list();
} catch (HibernateException e) {
logger.error("error at RenterDaoImpl.searchByNameAddress: " + e.getMessage());
} finally {
session.close();
}
return list;
}
My SQL called (SEARCH_BY_NAME_PERSON), use to search name and personalId:
SELECT
R.ID,
R.NAME,
R.ACCOUNT_ID,
R.OTP_CODE,
R.OS_TYPE,
R.MANCHINID,
R.TYPE,
R.PERSONAL_ID,
R.PHONENUMBER,
R.EMAIL,
R.ADDRESS,
R.DISTRICT_ID,
D.NAME AS DISTRICT_NAME,
R.CITY_ID,
C.NAME AS CITY_NAME
FROM
RENTER R
INNER JOIN
DISTRICT D
ON
R.DISTRICT_ID = D.ID
INNER JOIN
CITY C
ON
R.CITY_ID = C.ID
WHERE
LOWER(R.NAME)
LIKE
('%'||'?')
OR
R.PERSONAL_ID
LIKE
'%'||'%'
When I search with keyword: 'name' or 'personalId', I received an error result following as:
SqlExceptionHelper - SQL Error: 0, SQLState: S1009
SqlExceptionHelper - Parameter index out of range (1 > number of parameters, which is 0).
RenterDaoImpl - error at RenterDaoImpl.searchByNameAddress: could not execute query
How to fix this the problem ? Thank you so much !
You have this expression:
LIKE ('%'||'?')
I am guessing that you want '?' to be a parameter. But it is a lowly string, with just a single question mark.
Instead:
LIKE CONCAT('%', ?)
In general, in MySQL, you want to use CONCAT() for string concatenation.
I'm using EclipseLink as JPA implementation and I need to get multiple objects using the primary key (numeric id). But I also need to maintain the given id order.
Using native mySQL this kind of behaviour can be obtained using ORDER BY FIELD
SELECT id FROM table WHERE id IN(9,5,2,6) ORDER BY FIELD(id,9,5,2,6);
I'm now trying to replicate this query using JPA implementation. As already established from this thread, the ORDER BY FIELD is not supported, so I went to a more low-level approach using a JPA native query.
I'm try to reach this goal using a parameter query, instead of using a raw statement. The first implementation was like this
Class<?> clazz = ...;
List<Long> ids = ...;
EntityManagerFactory emf = ...;
EntityManager em = emf.createEntityManager();
String statement = "SELECT * FROM table WHERE id IN (?)";
Query createNativeQuery = em.createNativeQuery(statement, clazz);
createNativeQuery.setParameter(1, ids);
List resultList = createNativeQuery.getResultList();
As you can see the ORDER clause is not there yet, for the first step I just trying to make the parameter query work using the ids list with the IN operator. In the setParameter method I tried to provide the List object, a comma separated list (as string) but none of them works. At the end they all finish with a sql syntax error.
I also tried to play with the parenthesis, with or without, but nothing works.
Here some test I made
String statement = "SELECT * FROM " + tableName + " WHERE id IN (?)";
Query createNativeQuery = emJpa.createNativeQuery(statement, this.em.getClassObject());
createNativeQuery.setParameter(1, ids);
The query does not give any error, but no results given.
String statement = "SELECT * FROM " + tableName + " WHERE id IN (?)";
Query createNativeQuery = emJpa.createNativeQuery(statement, this.em.getClassObject());
createNativeQuery.setParameter(1, Joiner.on(",").join(ids));
Only one result is given, but 7 ids was provided to the query
From this topic I also tried using ?1 instead of ?, but no changes. Is there a way to make the nativeQuery working with a list of ids?
For the moment I'm using the full raw SQL statement
String joinedId = Joiner.on(",").join(ids);
String statement = "SELECT * FROM " + tableName + " WHERE id IN (" + joinedId + ") ORDER BY FIELD(id," + joinedId + ")";
Query createNativeQuery = emJpa.createNativeQuery(statement, this.em.getClassObject());
createNativeQuery.getResultList();
But at first I started with the parameter query for optimization and performance related of parsing each time the statement.
EDIT
With the suggestion of Chris I tried a TypedQuery using the FUNCTION operator (which is available because I'm using the latest EclipseLink). Here is the resulting code
List<Long> ids = ...;
Class<?> clazz = ...;
String statement = "SELECT e FROM " + clazz.getSimpleName() + " e WHERE e.id IN (:idList) ORDER BY FUNCTION('FIELD', e.id, :idList)";
EntityManagerFactory emf = ...;
EntityManager em = emf.createEntityManager();
TypedQuery<?> query = em.createQuery(statement, clazz);
query.setParameter("idList", ids);
List resultList = query.getResultList();
And here is the error while executing this code
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Operand should contain 1 column(s)
Error Code: 1241
Call: SELECT ... all the fields ... FROM webcontent_type WHERE (ID IN ((?,?,?,?,?,?,?))) ORDER BY FIELD(ID, (?,?,?,?,?,?,?))
bind => [14 parameters bound]
Query: ReadAllQuery(referenceClass=WebContentType sql="SELECT ... all the fields ... FROM webcontent_type WHERE (ID IN (?)) ORDER BY FIELD(ID, ?)")
EDIT 2
Tried without the parenthesis but there is still an error
SELECT e FROM FrameWorkUser e WHERE e.id IN :idList ORDER BY FUNCTION('FIELD', e.id, :idList)
I must say that with a list of one element the code works, but with another list of 10 elements there is an error
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Operand should contain 1 column(s)
Error Code: 1241
Call: SELECT .... FROM webcontent_type WHERE (ID IN (?,?,?,?,?,?,?)) ORDER BY FIELD(ID, (?,?,?,?,?,?,?))
bind => [14 parameters bound]
Query: ReadAllQuery(referenceClass=WebContentType sql="SELECT .... FROM webcontent_type WHERE (ID IN ?) ORDER BY FIELD(ID, ?)")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:382)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:473)
It seems that even w/o the parenthesis, the resulting statement has them
If you are going to use a native query, you must do it exactly like you would form the SQL for your database - this means you must break the list into its component parameters as JPA providers are not expected to change the SQL for you. Most providers handle lists in JPQL though, so "select e from Entity e where e.id in (:idList)" will work in EclipseLink.
Your missing bit is that 'FIELD' is not a JPQL construct. For this, you would have to use the JPQL 2.1 FUNCTION operator. Something like:
"Select e from Entity e where e.id in :idList order by FUNCTION('FIELD', e.id, :idList)"
#Override
public List<Sample> getSampleRecords() {
List<Sample> samples = null;
try {
Query query = entityManager
.createQuery(
"from Sample s order by s.createdAt desc")
.setLockMode(LockModeType.PESSIMISTIC_WRITE);
query.setMaxResults(100);
samples = query.getResultList();
} catch (Exception exceptionGetSample) {
logger.error("Exception while retrieving the records", exceptionGetSample);
}
return samples;
}
When I executed it with oracle db an error occur :
Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00907: missing right parenthesis
If I executed it only with lockmode parameter no error occurred.
If I executed it only with maxResult parameter no error occured.
I have used the eclipselink 2.5.0 library.
Please suggest some way so that I can use both the locking as well as can limit the records by using setMaxResults.
See the below link for temporay resolution to the issue
https://bugs.eclipse.org/bugs/show_bug.cgi?id=453208#c7
I'm trying to fetch the last entity that was inserted into the database, which I thought would be a very simple thing to do, but every query i try results in some sort of exception to get thrown
The code im using is:
#Override
public DataStoreMark getLastMark() {
String selectQuery = "from Mark";
Query query = em.createNativeQuery(selectQuery, DataStoreMark.class);
try {
return (DataStoreMark) query.getSingleResult();
} catch (NoResultException e) {
log.error("Couldn't find any Marks in the DataStore.");
}
return null;
}
This code however throws a PesistenceException:
org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from milestone' at line 1
And there is definitely a record in the database.
Any ideas?
You can use either of createQuery() and createSqlQuery() if it does not work:
1.
String selectQuery = "from Mark";
Query query = em.createQuery(selectQuery);
return (DataStoreMark) query.list().get(0);
2.
String selectQuery = "select * from Mark";
SQLQuery query = (SQLQuery) em.createSQLQuery(selectQuery);
query.addEntity(DataStoreMark.class);
return query.list();
I think hibernate does not tell about last entity added to the database. Alternatively you can write the query specific to your db and run it using createSqlQuery() as shown above.
I tried to execute the following oracle query using JdbcTemplate in Java:
select RESOURCE_ID
from REPRO_PRINTING_JOB
where (USER_ID=? and PRINTING_CENTER_ID=?)
group by RESOURCE_ID
union all
select RESOURCE_ID
from REPRO_PRINTING_JOB_OLD
where (USER_ID=? and PRINTING_CENTER_ID=? and STATE='CM')
group by RESOURCE_ID
The query is working perfectly in oracle query browser but its not working during the execution in java. What could be the source of this problem? I´ve heard something about Jdbc cannot handle case sensitive. Is this true?
ADDED JAVA CODE(CORRECTED) :
I call the query using getStatement() which retrieves the query from external source(a properties file) as belows :
public List<PrintingJob> getPrintingJobsByCreatedUserId(String createdUserId, String userId) {
if(log.isDebugEnabled()) {
log.debug("getReportItemsByUserId(" + createdUserId + "," + userId + ")");
}
try {
System.out.println("getPrintingJobsByResourceId : createdUserId :"+createdUserId+",userId : "+userId);
return getJdbcTemplate().query(getStatement("gen.report.userid"),
new Object[]{createdUserId, userId, createdUserId, userId},
new PrintingJobMapper());
} catch (DataAccessException ex) {
log.error("Error executing query: " + ex.getClass() + ":" + ex.getMessage());
return null;
}
}
The reason you are getting this error is because it is an error from the JDBC driver or java.sql API, not from the database. Note the lack of ORA-XXXXX in the exception message. If it contained that, then it would be related to syntax, invalid name or something that would go wrong on the database server side.
SQLExceptions are thrown in java for anything related to the SQL api, not just from errors that occur when statements are sent over the connection... In this case, it is likely that the query in fact ran correctly but the result set API is giving you errors (rs.getLong("blah"), where column "blah" doesn't exist in your select) is causing problems.