Hibernate学习笔记

1 】判断用户名是否重复

public   boolean  isDuplicate(String name)  {
    
// TODO Auto-generated method stub
    List ls=this.getHibernateTemplate().find("from com.datalook.ykt.model.Country as c where c.name=?",name);
    
if(ls.size()>0)
     
return true;
    
else 
     
return false;
}


2 】通过一个字段id得到一个实体

public  Country getCountry( long  id) throws  NullPointerException {
    
// TODO Auto-generated method stub
    List ls=this.getHibernateTemplate().find("from com.datalook.ykt.model.Country as c where c.id=?",new Long(id));  
    
if(ls.size()>0)
     
return (Country)ls.get(0);
    
else
     
return null;
}











3 】HIbernate查询 更新 操作new Object[]

public  List modifyCardNo( long  oldcardno,  long  newcardno)  {
   
// TODO Auto-generated method stub
   List result=new ArrayList();  
   List ls
=this.getHibernateTemplate().find("from com.datalook.ykt.model.Trjn as trjn where trjn.cardNo=? and trjn.message=?",new Object[]{new Long(oldcardno),GolbalParameter.OpenCount_Common});
   
if(ls.size()>1||ls.size()==0){
    
throw new ServiceLogicException("-020"); //无满足条件记录! ----定义新的异常类型
   }

   
else{
    Trjn trjn
=(Trjn)ls.get(0);   
    List rs
=this.getHibernateTemplate().find("from com.datalook.ykt.model.Trjn as trjn where trjn.cardNo=? and trjn.jourNo!=? ",new Object[]{new Long(oldcardno),new Long(trjn.getJourNo())});
    
if(rs.size()>0)
     
throw new ServiceLogicException("-020");
  
    
this.getHibernateTemplate().bulkUpdate("update com.datalook.ykt.model.Trjn as trjn set trjn.cardNo=? where trjn.jourNo=? and trjn.cardNo=?",new Object[]{new Long(newcardno),new Long(trjn.getJourNo()),new Long(oldcardno)});
   
    
int i=this.getHibernateTemplate().bulkUpdate("update com.datalook.ykt.model.UserMaster as um set um.cardNo=? " +
      
"where um.cardNo=?",new Object[]{new Long(newcardno),new Long(oldcardno)});
    
if(i<1)
     
throw new ServiceLogicException("-020"); //无满足条件记录! 
    result.add(0,"0000");
    result.add(
1,"影响:"+i+"条记录!");   
   }
 
   
return result;
}


4 】Hibernate的基本sql操作  {*}    addEntity(,,)

/**
   * 档案查询
   * 
@param cardno 卡号
   * 
@return get(0)操作成功消息代码 get(>1)为档案集合 其结构为get(1)为list类型list下的get(0)为UserMaster对象get(1)为UserInfo对象
   * 
@throws ServiceLogicException
   
*/


public  List getUserMasterInfo( long  cardno)  throws  ServiceLogicException  {
   
// TODO Auto-generated method stub
   List result =new ArrayList();
   String sql
="select {um.*},{ui.*} from User_Master um ," +
     
"user_info ui where um.UserId=ui.UserId and um.CardNo="+cardno;
   Session session
=this.getSessionFactory().getCurrentSession();
   SQLQuery sqlquery
=session.createSQLQuery(sql).addEntity("um",UserMaster.class).addEntity("ui",UserInfo.class);
   List ls
=sqlquery.list();
   
if(ls.size()<1)
    
throw new ServiceLogicException("-020"); //无满足条件记录!  
   result.add(0,"0000");
   
int i=1;
   Iterator iter
=ls.iterator();
   List objls
=new ArrayList();
   Object[] obj
=new Object[2];
   
while(iter.hasNext()){
    obj
=(Object[])iter.next();
    objls.add(
0,obj[0]);
    objls.add(
1,obj[1]);
    result.add(i,objls);
    i
++;
   }

   
return result;
}


5
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值