【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】
本文提供了一系列Hibernate框架下的数据库操作示例,包括判断用户名是否重复、通过ID获取实体、更新记录等常见操作,并展示了如何处理异常。
432

被折叠的 条评论
为什么被折叠?



