Hibernate:执行getHibernateTemplate()时发生映射异常。更新查询
获取异常原因: 执行此代码时:Hibernate:执行getHibernateTemplate()时发生映射异常。更新查询,hibernate,exception,mapping,Hibernate,Exception,Mapping,获取异常原因: 执行此代码时: public void confirmUser(String username,String confirmationCode){ getHibernateTemplate().update("UserDetails set confirmed=true where username=? and confirmationCode=?",new Object[]{username,confirmationCode}); } 编辑 这意味着,我的hbm.xm
public void confirmUser(String username,String confirmationCode){
getHibernateTemplate().update("UserDetails set confirmed=true where username=? and confirmationCode=?",new Object[]{username,confirmationCode});
}
编辑
这意味着,我的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>
问题是,如何使用一组参数执行更新方法,因为getHibernateTemplate.update假定将对象传递给该方法,而不是SQL查询。为了通过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不允许命名参数,因此已完成。应传递对象。尝试使用完全限定的类名。并添加更多细节。@slayer_b您是否建议在其前面加上包声明?是的。发布您的实体、配置、dao,我指的是整个类。因为配置中似乎有错误。我已更新了帖子。实际上其他查询工作正常,请使用UserDetails Mapping查看编辑部分。可能问题出在getHibernateTemplate.update方法中。找不到合适的示例。大多数示例中都使用session.saveOrUpdate。
<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>
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();