休眠更新查询:如何设置默认值



我有两个实体类

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

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

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

我是冬眠的新手。

你可以做这样的事情:

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();

最新更新