【问题标题】:Hibernate: MappingException when execute getHibernateTemplate().update queryHibernate:执行 getHibernateTemplate().update 查询时出现 MappingException
【发布时间】:2011-12-14 16:29:26
【问题描述】:

得到异常原因:

org.hibernate.MappingException: Unknown entity: UserDetails set confirmed=true where username=? and confirmationCode=?

这段代码何时执行:

public void confirmUser(String username,String confirmationCode){
    getHibernateTemplate().update("UserDetails set confirmed=true where username=? and confirmationCode=?",new Object[]{username,confirmationCode});
}

编辑

This query works OK:
    public String getUserMail(String username) {
        return (String) DataAccessUtils.uniqueResult(getHibernateTemplate().find(
                "select mail from UserDetails where username=?", new Object[] { username }));
    }

这意味着,我的hbm.xml 也应该没问题:

<hibernate-mapping>
    <class name="model.UserDetails" table="users">
        <id name="id">
            <generator class="increment"/>
        </id>
        <property name="username" column="username"/>
        <property name="password" column="password"/>
        <property name="enabled" column="enabled"/>
        <property name="mail" column="mail"/>
        <property name="city" column="city"/>
        <property name="confirmed" column="confirmed"/>
        <property name="confirmationCode" column="confirmation_code"/>

        <set name="authorities" cascade="all" inverse="true">
            <key column="id" not-null="true"/>
            <one-to-many class="model.Authority"/>
        </set>

    </class>
</hibernate-mapping>

问题是,如何使用一组参数执行update 方法,因为getHibernateTemplate().update 假定将Object 传递给方法,而不是SQL 查询。

【问题讨论】:

  • 尝试使用完全限定的类名。并添加更多细节。
  • @slayer_b 你建议在它前面加上包声明吗?
  • 是的。发布您的实体,配置,dao 我的意思是整个班级。因为似乎配置中有错误。
  • 我已经更新了帖子。实际上其他查询工作正常,使用UserDetails 映射(请参阅编辑部分)。
  • 也许,问题出在getHibernateTemplate().update方法上。找不到合适的例子。在大多数例子中使用session.saveOrUpdate

标签: hibernate exception mapping


【解决方案1】:

为了通过Hibernate ORM执行更新,我使用了这样的构造:

SessionFactory sf = getHibernateTemplate().getSessionFactory();
Session s = sf.openSession();
Query q = s.createQuery("UserDetails set confirmed=true
 where username:username and confirmationCode=:confirmationCode");
q.setString("username", username);
q.setString("confirmationCode", confirmationCode);
q.executeUpdate();

已经完成,因为 getHibernateTemplate().update 不允许命名参数。
应该有对象传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多