首页 > 其他 > 详细

Hibernate 之 增删改查

时间:2014-01-21 01:23:40      阅读:416      评论:0      收藏:0      [点我收藏+]

Hibernate之增删查改(ORACLE数据库)

前提:SCHOOL的表结构如下,
SQL> describe school;
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ------------------------
 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(100)

1.
向SCHOOL表添加100条记录
void add()
{
 Transaction ts = null;
 Session sess = HibernateSessionFactory.getSession();
 
 for(int i=0; i<100; i++)
 {
  
  try{
   
   ts = sess.beginTransaction();
   
   School bkg = new School();
   bkg.setId(new BigDecimal(i));
   bkg.setName("北京大学");
   sess.save(bkg);
   
   ts.commit();
  }
  catch(ConstraintViolationException e)
  {
   System.out.println(e.getMessage());
  }
  
 }
 
 sess.close();
}

2.删除前50条记录
void Delete()
{
 Transaction ts = null;
 Session sess = HibernateSessionFactory.getSession();
 for(int i=0; i<50; i++)
 {
  
  try{
   
   ts = sess.beginTransaction();
   
   School bkg = (School)sess.get(School.class, new BigDecimal(i));
   sess.delete(bkg);
   
   ts.commit();
  }
  catch(PropertyValueException e)
  {
   System.out.println(e.getMessage());
  }
  
 }
 sess.close();
}

3.修改前25条记录的name为“清华大学”
void Update()
{
 Transaction ts = null;
 Session sess = HibernateSessionFactory.getSession();
 for(int i=50; i<75; i++)
 {
  
  try{
   
   ts = sess.beginTransaction();
   
   School bkg = (School)sess.get(School.class, new BigDecimal(i));
   bkg.setName("北京大学");
   sess.update(bkg);
   
   ts.commit();
  }
  catch(PropertyValueException e)
  {
   System.out.println(e.getMessage());
  }
  
 }
 sess.close();
}
4.查询id=86的记录
void Search()
{
 Transaction ts = null;
 Session sess = HibernateSessionFactory.getSession();
 
 try{
  
  School bkg = (School)sess.get(School.class, new BigDecimal(86));
  System.out.println(bkg.getId() + "---" + bkg.getName());
 }
 catch(PropertyValueException e)
 {
  System.out.println(e.getMessage());
 }
 
 sess.close();
}

 

5.HQL查询方式

void HQLSearch() {

 
  Transaction ts = null;
  Session sess = HibernateSessionFactory.getSession();
  
  try{
   
   Query qry = sess.createQuery("from School");
   List<School> lst = qry.list();
   for(School sl: lst)
   {
    System.out.println("Name = "+sl.getName()+ "   id=" +sl.getId());
   }
  }
  catch(PropertyValueException e)
  {
   System.out.println(e.getMessage());
  }

  sess.close();
 
 }

 

6.SQL查询

void SqlSearch()

 {
  // TODO Auto-generated method stub
 
  Transaction ts = null;
  Session sess = HibernateSessionFactory.getSession();
  
  try{
   
   List<School> sq = sess.createSQLQuery("select * from school where id <52").addEntity(School.class).list();
   
   
   for(School sl: sq)
   {
    System.out.println(sl.getName()+sl.getId());
   }
  }
  catch(PropertyValueException e)
  {
   System.out.println(e.getMessage());
  }

  sess.close();
 
 }

 

Hibernate 之 增删改查

原文:http://blog.csdn.net/hellochenlian/article/details/18312639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!