原來HIBERNATE還可以這樣用啊。。。動態表名

來源:互聯網
上載者:User

最近發現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();
  }
}

這個類是每天建立一張表,並往裡寫資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.