HQL更新查询 [英] HQL update query

查看:144
本文介绍了HQL更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行一个update-HQL-query时遇到问题.

I've got problem to exceute one update-HQL-query.

我要编辑保存在配置文件实体中的值,但是没有运气.

I want to edit values saved in profile entity, but with no luck.

我的实体如下所示

@Entity
@Table(name = "users", catalog = "testdb")
public class UserEntity implements java.io.Serializable {

private Integer id;
private String username;
private String password;
private Profile profile;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
public Profile getProfile() {
    return this.profile;
}

public void setProfile(Profile profile) {
    this.profile = profile;
}
}


@Entity
@Table(name = "profiles", catalog = "testdb")
public class Profile implements java.io.Serializable {
private Integer id;
private UserEntity user;
private String firstName;
private String lastName;

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public UserEntity getUser() {
    return this.user;
}

public void setUser(UserEntity user) {
    this.user = user;
}
}

在控制器中,我要执行以下更新查询:

In controller I want to execute the following update query:

@RequestMapping(value = "/profile", method = RequestMethod.POST)
public String profilePost(ModelMap model, @ModelAttribute("user") UserEntity user) {

    Session session = HibernateUtil.getSessionFactory().openSession();

session.beginTransaction();

Query query = session.createQuery("update UserEntity u set u.profile.firstName =     :firstName"
                    + " where u.username = :username");

query.setParameter("firstname", "john");
query.setParameter("username", user.getUsername());
int result = query.executeUpdate();

session.getTransaction().commit();

return "redirect:/login";
}

您能给我一些建议,如何看待这个查询:)?

could You give me some advice how should lok this query:)?

推荐答案

来自文档:

在从句中只能命名一个实体.它可以, 但是,请使用别名.如果实体名称是别名,则任何属性 引用必须使用该别名进行限定.如果实体名称是 没有别名,那么任何属性引用都是非法的 合格的.

There can only be a single entity named in the from-clause. It can, however, be aliased. If the entity name is aliased, then any property references must be qualified using that alias. If the entity name is not aliased, then it is illegal for any property references to be qualified.

不能在批量HQL中指定隐式或显式连接 询问.子查询可在何处子句,何处 子查询本身可能包含联接.

No joins, either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.

像Hibernate这样的ORM的想法是避免此类更新查询.您从会话中获得一个持久性实体,对其进行修改,然后Hibernate透明地将更新保存到数据库中:

The idea of an ORM like Hibernate is to avoid such update queries. You get a persistent entity from the session, you modify it, and Hibernate transparently saves the updates to the database:

Query query = session.createQuery("select p from Profile p where p.user.username = :username");
Profile p = query.setString("username", user.getUsername()).uniqueResult();
p.setFirstName("john");

这篇关于HQL更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆