Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named query not known - Annotations & Hibernate

Tags:

java

hibernate

I am getting the following exception:

org.springframework.orm.hibernate3.HibernateSystemException: Named query not known:

Entity class header:

@Entity
@NamedNativeQuery( callable = true, name = "_Foo_SP", query = "call _Foo()", readOnly = true, resultClass = Foo.class )
public class Foo {
   //...properties omitted for brevity
}

In the hibernate.cfg.xml:

    <mapping
        class="com.company.test.Foo" />

And in a test class:

private static HibernateTemplate HIBERNATE_TEMPLATE;

public static void main( final String[] args ) {
    HIBERNATE_TEMPLATE =
        new HibernateTemplate( new AnnotationConfiguration().addAnnotatedClass( Foo.class ).configure().buildSessionFactory() );
    new HibernateTest().test();
}

public void test() {
    List findByNamedQuery = HIBERNATE_TEMPLATE.findByNamedQuery( "_Foo_SP" );
    for( Object object : findByNamedQuery ) {
        System.out.println( object );
        System.out.println( object.getClass().getName() );
    }
}

I had this working without annotations (eg: with the mapping in a mapping file) but it seems more intuitive to simply use the JPA annotations to declare mappings - but I can't seem to get it to work.

What am I doing wrong here? Is what I'm trying to do even possible? It seems I'm not the only one to encounter this, see: here.

I'm using hibernate 3.5.6-FINAL FWIW.

TIA

like image 409
javamonkey79 Avatar asked Oct 27 '10 19:10

javamonkey79


1 Answers

The problem was that I was using the wrong @Entity class. When I used:

org.hibernate.annotations.Entity

I would get the problems above. However, once I switched to:

javax.persistence.Entity

It works!

like image 87
javamonkey79 Avatar answered Nov 04 '22 06:11

javamonkey79