jjklm

HIBERNATE小结

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

posted on 2006-09-13 17:11 天涯孤客 阅读(85) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: