2012-06-14 34 views
0

我有两个实体类Hibernate更新查询:如何设置默认值?

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

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

我如何可以设置所有user.userType其中user.userType=nullnew UserType()通过HQL更新查询?

我是hibernate的新手。

+1

阅读**更新查询示例**此链接的部分:http://www.mkyong.com/hibernate/hibernate-query-examples-hql/ –

回答

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

感谢Spaeth,我只是想关于query.setString和query.setInterger来设置ID和用户名 – sanu