Hibernate 会话工厂找不到 DAO 注释中定义的 NamedQuery
我将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将命名查询放在您的实体上,而不是放在 dao
此处是一个例子
you have to put the named queries on your entity, not on the dao
here is an example
如果您使用 @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.