hibernate需要查询中映射类的完整路径

时间:2014-07-19 14:50:41

标签: java spring hibernate annotations

我在路径com.me.model.User处映射了类。用户DAO位于com.me.daoImpl.UserDaoImpl

dao看起来像这样:

package com.me.daoImpl;

//other imports
import com.me.model.User;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    SessionFactory sessionFactory;

    //other methods...

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers() {
        Session session = sessionFactory.openSession();
        try {
            return session.createQuery("from com.lime.model.User").list();
        } finally {
            session.close();
        }
    }

}

我的问题是如何让hibernate知道modelcobject类?我已经在上面看到了User的导入,但是我必须在createQuery()方法中给出完整的路径。这显示错误:无法解析符号User

return session.createQuery("from User").list();

2 个答案:

答案 0 :(得分:0)

您可以在hibernate配置文件中映射类和包,如

<hibernate-configuration>
    <session-factory>
        <mapping class="com.me.model.User" />

要在spring中使用此配置,请将此属性添加到会话工厂bean

<property name="configLocation" value="classpath:hibernate.cfg.xml"/>

hibernate.cfg.xml应该在类路径上。

答案 1 :(得分:0)

Hibernate 5也有类似的问题,请确保使用正确的JDK版本。 我将我的数字从8更改为11,并且有效。

还必须在<mapping class="com.example.ClassName" />文件中包含hibernate.cfg.xml和中提琴,编译器不再要求输入完全限定的名称。.