命名查询未知 - 注释&过冬 [英] Named query not known - Annotations & Hibernate

查看:158
本文介绍了命名查询未知 - 注释&过冬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  org.springframework.orm.hibernate3.HibernateSystemException:命名查询未知:

实体类标题:

  @Entity 
@NamedNativeQuery(callable = true,name =_Foo_SP,query =call _Foo(),readOnly = true,resultClass = Foo.class)
public class Foo {
//...properties为简洁省略
}

在hibernate.cfg.xml:

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

在测试类中:

  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());


$ / code $ / pre

我有这个工作没有注解(例如:与映射到映射文件中),但是使用JPA注释声明映射似乎更直观 - 但我似乎无法使其工作。



我在这里做错了什么?我试图做甚至可能吗?看来我不是唯一遇到此问题的人,请参阅:此处



我使用hibernate 3.5.6-FINAL FWIW。



TIA

解决方案

问题是我使用了错误的@Entity类。当我使用时:

  org.hibernate.annotations.Entity 

我会遇到上述问题。但是,一旦切换到:

  javax.persistence.Entity 

有效!


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!

这篇关于命名查询未知 - 注释&amp;过冬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆