2012-06-14 70 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