1

I have a method which is trying to use HQL to do a rather complex query within the database. My entity classes are all mapped with annotations and are discovered during compilation, not explicitly listed.

In the code which follows:

    List<Message> ms = ((Session) entityManager.getDelegate())
            .createCriteria(MessageCRI.class).list();
    log.info("Got " + ms.size() + " CRIs");

    List<Message> msgs = ((Session) entityManager.getDelegate())
            .createQuery(
                    "from MessageCRI c where c.rfdId in (select id from MessageRFD rfd where rfd.ssrId = :ssrId)")
            .setParameter("ssrId", ssrId).list();

The first call succeeds -- I get a message in the log stating "Got 2 CRIs" or somesuch. The very next call, however, fails, producing a stackdump with a root cause of "org.hibernate.hql.ast.QuerySyntaxException: MessageCRI is not mapped"

What could I be missing?

Bret
  • 1,455
  • 2
  • 16
  • 27

2 Answers2

3

Is your @Entity annotation receiving a name? (In that case, use it's name).

Out of subject but since you are using JPA EntityManager, why don't you stick with JPQL syntax? And if you are using JPA 2, you can even leverage it's type-safe Criteria API. Native syntax may still be needed for very specific situations, but following the standards when you have the option is always a good choice.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • I would have used the Criteria API, but this looked more complex than I could easily and clearly deal with using Criteria. I may revisit that decision. I'm also recompiling now to use the name I set (so that the table name would match an existing table). – Bret May 13 '11 at 18:03
  • Good lucky. Yeah, verbosity is a valid reason to not step into something (still, I've got kinda used to the criteria API). As for JPQL, it is about as verbose as HQL. And you can always port it to a different ORM solution (like EclipseLink, or maybe even something more exotic such as DataNucleus JPA implementation for Google Datastore :D. We never know...) – Anthony Accioly May 13 '11 at 18:13
1

Entity in HQL is identified by its entity name. By default it matches the short name of its class, but it can be overriden by name attribute of @Entity annotation. Make sure you don't override it.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • I did override it so that my table name would match an existing, legacy table. I'm recompiling to use that name now, with fingers crossed. – Bret May 13 '11 at 18:03
  • 1
    @Bret: Table name is specified in `@Table(name = ...)`. `@Entity(name = ...)` has nothing to do with it. – axtavt May 13 '11 at 18:16