Annotations & 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


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!

链接地址: http://www.djcxy.com/p/48494.html

上一篇: 反序列化XML时忽略指定的编码

下一篇: 注释和休眠