Hibernate 会话工厂找不到 DAO 注释中定义的 NamedQuery

发布于 2024-12-09 19:46:09 字数 2223 浏览 8 评论 0原文

我将 Spring 与 Hibernate 一起使用。在我的 DAO 中,我定义了一个会话工厂找不到的 NamedQuery,尽管我已将该 DAO 的包添加到 packagesToScan 中。

我的 DAO:

/**
 * 
 */
package org.lalala.service.mytest;

import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import  org.lalala.objects.Unit;


@NamedQueries
({
    @NamedQuery
    (
        name="unit.findById",
        query="from Unit u where u.unitid = :unitId"
    )
})
public class MyTestDaoImpl implements MyTestDao
{

    private SessionFactory sessionFactory;


    public MyTestDaoImpl(SessionFactory sessionFactory)
    {
        super();
        this.sessionFactory = sessionFactory;
    }



    /* (non-Javadoc)
     * @see org.lalala.service.mytest.MyTestDao#getRandomUnit()
     */
    @Override
    public Unit getRandomUnit()
    {
        long unitid = 2891;
        try {
            Session session = sessionFactory.getCurrentSession();
            session.beginTransaction();

            Query query = session.getNamedQuery("unit.findById");
            query.setParameter("unitId", unitid);

            Unit unit = (Unit) query.uniqueResult();

            session.getTransaction().commit();

            return unit;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }    

}

这里是用于提供会话工厂的 Spring 配置方法:

@Bean
public AnnotationSessionFactoryBean  getSessionFactory() {
    final AnnotationSessionFactoryBean  sessionFactory = new  AnnotationSessionFactoryBean ();
    sessionFactory.setDataSource(getDataSource());

    String[] packages = new String[]{"org.lalala.avalon.service.mytest"};

    sessionFactory.setAnnotatedPackages(packages);

    Properties properties = new Properties();
    properties.put("hibernate.current_session_context_class", "thread");
    sessionFactory.setHibernateProperties(properties);

    return sessionFactory;
}

这里抛出的异常:

org.hibernate.MappingException: Named query not known: unit.findById

由于一些类似的 stackoverflow 问题,我用 JPA 等效项替换了 hibernate @NamedQuery 注释。没有帮助。

I use Spring along with Hibernate. In my DAO, I defined a NamedQuery which is not found by the session factory, although I have added the package of that DAO to the packagesToScan.

My DAO:

/**
 * 
 */
package org.lalala.service.mytest;

import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import  org.lalala.objects.Unit;


@NamedQueries
({
    @NamedQuery
    (
        name="unit.findById",
        query="from Unit u where u.unitid = :unitId"
    )
})
public class MyTestDaoImpl implements MyTestDao
{

    private SessionFactory sessionFactory;


    public MyTestDaoImpl(SessionFactory sessionFactory)
    {
        super();
        this.sessionFactory = sessionFactory;
    }



    /* (non-Javadoc)
     * @see org.lalala.service.mytest.MyTestDao#getRandomUnit()
     */
    @Override
    public Unit getRandomUnit()
    {
        long unitid = 2891;
        try {
            Session session = sessionFactory.getCurrentSession();
            session.beginTransaction();

            Query query = session.getNamedQuery("unit.findById");
            query.setParameter("unitId", unitid);

            Unit unit = (Unit) query.uniqueResult();

            session.getTransaction().commit();

            return unit;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }    

}

And here the Spring config method for providing a session factory:

@Bean
public AnnotationSessionFactoryBean  getSessionFactory() {
    final AnnotationSessionFactoryBean  sessionFactory = new  AnnotationSessionFactoryBean ();
    sessionFactory.setDataSource(getDataSource());

    String[] packages = new String[]{"org.lalala.avalon.service.mytest"};

    sessionFactory.setAnnotatedPackages(packages);

    Properties properties = new Properties();
    properties.put("hibernate.current_session_context_class", "thread");
    sessionFactory.setHibernateProperties(properties);

    return sessionFactory;
}

And here the exception which is thrown:

org.hibernate.MappingException: Named query not known: unit.findById

Because of some similar stackoverflow questions, I replaced the hibernate @NamedQuery annotation with JPA equivalent. Did not help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-12-16 19:46:09

您必须将命名查询放在您的实体上,而不是放在 dao

此处是一个例子

you have to put the named queries on your entity, not on the dao

here is an example

蓝天白云 2024-12-16 19:46:09

如果您使用 @MappedSuperclass 注释您的 DAO,那么您可以将您的 NamedQueries 放入 DAO 中。不要忘记将 DAO 的包或 DAO 类本身添加到带注释的包/类列表中。

If you annotate your DAO with @MappedSuperclass then you can put your NamedQueries in the DAO. Don't forget to add the package of the DAO or the DAO-class itself to the list of annotated packages/classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文