Hibernate更新查询:如何设置默认值?

时间:2012-06-14 18:24:44

标签: java hibernate hql

我有两个实体类

class User { 
    Integer userId;
    String userName;
    UserType userType;
}

class UserType {
    Integer typeId;
    String type;
    Set<User> userList;
}

如何通过HQL更新查询在new UserType()所有user.userType中设置user.userType=null

我是hibernate的新手。

1 个答案:

答案 0 :(得分:4)

您可以执行以下操作:

UserType userType = /* code to get already persisted userType */;

Query query = session.createQuery("update User u set u.userType = :newType where u.userType is null");
query.setParameter("newType", userType);
int result = query.executeUpdate();