梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks

最近发现HIBERNATE可以动态更换表名,真不简单,下面把原代码贴出来供参考:

public class ReportDBApi {

  private SessionFactory sessionFactory = null;
  public ReportDBApi(){
    createSession();
  }

  public void createSession(){
    Date date = new Date();
    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
    String now_time = simpledateformat.format(date);
    String tablename = "TBL_REPORT_STATUS_20050707";
    tablename = "TBL_REPORT_STATUS_" + now_time;
    try {
      Configuration cfg = new Configuration().addClass(cn.sports.vas.sms.unicom.TblReportStatus.class).configure();

      Table table = cfg.getClassMapping(TblReportStatus.class).getTable();
      table.setName(tablename);
      cfg.getClassMapping(TblReportStatus.class).setTable(table);
      sessionFactory  = cfg.buildSessionFactory();
    }
    catch (MappingException ex) {
      ex.printStackTrace();
    }catch (HibernateException ex) {
      ex.printStackTrace();
    }
  }

  public void insertPO(TblReportStatus po) throws HibernateException {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.save(po);
    tx.commit();
    session.close();
  }

  public void closeSession() throws HibernateException {
    sessionFactory.close();
  }
}


这个类是每天新建一张表,并往里写数据。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jljf_hh/archive/2005/07/14/424510.aspx

posted on 2009-07-16 23:20 HUIKK 阅读(1819) 评论(1)  编辑  收藏

评论

# re: Hibernate 动态表名 2010-05-31 11:54 fff
mei jishu hanliang  回复  更多评论
  


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


网站导航: