3

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

guido
  • 18,864
  • 6
  • 70
  • 95
Biscuit128
  • 5,218
  • 22
  • 89
  • 149
  • Write complete code of `EntityManager` also – Bhavik Ambani Nov 11 '12 at 11:43
  • 3
    it should be _"from Trade"_ (uppercase T) if Trade is the name of the mapped class – guido Nov 11 '12 at 11:44
  • @guido this indeed was the correct answer - thank you so much – Biscuit128 Nov 11 '12 at 11:49
  • 2
    @guido: please turn your comment into an answer. I added a bit of more details, but the credit should go to you. *SkyR*, please do not accept my answer, I wasn't first :-). – Tomasz Nurkiewicz Nov 11 '12 at 11:54
  • @TomaszNurkiewicz thanks, I just added a comment because it could have been just a typo – guido Nov 11 '12 at 12:02
  • Actually, @guido didn't mention the need for " t" on the end, which is what was the problem causing a grammar exception (having a bad entity name would get a `QuerySyntaxException: trade is not mapped`). – Tom Anderson Nov 11 '12 at 12:04
  • Hmm. Except for me, with Hibernate 4.1.4, "from Trade" works fine, even without the " t". A mystery remains here ... – Tom Anderson Nov 11 '12 at 12:08
  • @TomAnderson that's actually something that only happens using JPA over hibernate I think. Minimal JPQL query would also include SELECT (_SELECT e FROM Entity [AS] e_); but something just like "from Class" is valid in HQL and (mistakenly? I think so) works in hibernate's JQPL. – guido Nov 11 '12 at 12:10
  • @guido: Ah yes, that's possible. In any case, you're right, it's not standard JPQL, so we shouldn't worry too much about it! – Tom Anderson Nov 11 '12 at 12:18

2 Answers2

10

It should be "from Trade" (uppercase T) as Trade is the name of the mapped class.

guido
  • 18,864
  • 6
  • 70
  • 95
6

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
Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674