注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

我的博客

 
 
 

日志

 
 
关于我

砍过人吸过粉站在路边接过吻 当过兵站过岗耍过流氓入过党 上过班下过岗打过领导得过奖

网易考拉推荐
GACHA精选

MyEclipse+Hibernate 3--Operatation with database, update records  

2007-01-07 15:53:06|  分类: 网站设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

MyEclipse+Hibernate 3--Operatation with database, update records

Submitted by leelight on Wed, 2006-12-13 17:29.

If you get the data from the same one Session and want to update it right now, just use setXXX() and call session.flush(), and the new data will be inputted into the session. We have used this method in chapter before.

If you has opened one session to get he data from it and then closed the session after finished. How to update the data? Another new seesion should be opened, and call update() to update the database. For example,  the first record name is "1", it will be replaced with "lee":

package test;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import user.User;

public class TestUpdateRecord {

    public static void main(String[] args) {
       
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();        

        String query = "from User";
        Query q = session.createQuery(query);       
        List usersList = q.list();
        //close session, the user has queried the database
        session.close();
       
        User updatedUser = null;
       
        System.out.println( usersList.size() + " user(s) were found:" );
        for ( Iterator iter = usersList.iterator(); iter.hasNext(); ) {
           User user = (User) iter.next();
           if(updatedUser == null)
               updatedUser = user;
           System.out.println("usernames: "+user.getUsername()+" password: " +user.getPassword() );
        }               
       
        //the user want to update the record now, from 1 to lee
        updatedUser.setUsername("lee");
       
        //open new session
        Session newsession = sf.openSession(); 
       Transaction newTransaction = newsession.beginTransaction();
        //update the data
        newsession.update(updatedUser);
   
        Query q1 = newsession.createQuery(query);   
        usersList = q1.list();
       
        for ( Iterator iter = usersList.iterator(); iter.hasNext(); ) {
               User user = (User) iter.next();
               System.out.println("usernames: "+user.getUsername()+" password: " +user.getPassword() );
            }   
       
        newTransaction.commit();
        newsession.close();
        sf.close();
    }

}

3 user(s) were found:
usernames: 1 password: 1
usernames: 1 password: 1
usernames: mway password: mway

usernames: lee password: 1
usernames: 1 password: 1
usernames: mway password: mway

The record could be deleted when delete() was called. The first record will be deleted.

       //delete the data
        newsession.delete(updatedUser);
Ref:
http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=1478
  评论这张
 
阅读(264)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016