Reputation: 5398
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
Upvotes: 3
Views: 8343
Reputation: 19194
It should be "from Trade"
(uppercase T) as Trade is the name of the mapped class.
Upvotes: 10
Reputation: 340733
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
Upvotes: 6