1.
public Group findByID(long groupID) {
Session sess = HibernateUtil.getSystemSession();
try {
return (Group) sess.get(Group.class, new Long(groupID));
} catch (HibernateException e) {
throw new SystemException(e);
}
}
2.
public Group findTopGroup() {
Session sess = HibernateUtil.getSystemSession();
try {
String hql = "from Group group where group.groupId = 100000";
Query query = sess.createQuery(hql);
List list = query.list();
Group group = null;
if (list.size() > 0) {
group = (Group) list.get(0);
}
return group;
} catch (HibernateException e) {
throw new SystemException(e);
}
}
3. 延迟加载:Hibernate.initialize(permission.getFunctionList());
4. 匿名类做参数
transTemplate.execute(new TransactionCallback() {
public Object doBusiness() throws Exception {
UserDAO userDAO = DAOFactory.createUserDAO();
UserSession session = new UserSession();
//调用UserDAO的findByID方法取得原有信息newUser
User newUser = userDAO.findByID(user.getUserId());
//设置newUser的userPassword值为新密码
newUser.setUserPassword(user.getUserPassword());
//调用UserSession的modifyUser方法
session.modifyUser(newUser);
return null;
}}
);