Hibernate dynamically creating database table names in several ways

Source: Internet
Author: User

The amount of data in a database is large, but it is not possible to delete it while also optimizing the program to retrieve the data time.

A: There are many ways such as creating database table partitions, creating indexes, stored procedures, etc. I use this method of dynamically creating database Tables .
can be implemented without creating table partitions, such as the logging table will be the default table suffix dynamic append yyyy-mm, such as the article publishing table to the user name as a suffix dynamic append ;
The way to create database tables dynamically is to specifically analyze specific problems, such as using the CREATE TABLE table name _dynamicstr (...) directly in JDBC.
Article Publishing System DYNAMICSTR can use registered login username , log table Dynamicstr can use ' Log_ ' +new simpledateformat ("Yyyymm"). Format (new Date ());

The following example is an example of dynamically creating a database table using Hibernate and, if interested, can extract the following code into a dynamically created table Utils.
Public classReportdbapi {PrivateSessionfactory sessionfactory =NULL; PublicReportdbapi () {createsession (); } Public voidcreatesession () {Date Date=NewDate (); SimpleDateFormat SimpleDateFormat=NewSimpleDateFormat ("YyyyMMdd"); String Now_time=Simpledateformat.format (date); String TableName= "tbl_report_status_20050707"; TableName= "Tbl_report_status_" +Now_time; Try{Configuration cfg=NewConfiguration (). 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 voidINSERTPO (Tblreportstatus po)throwsHibernateexception {Session session=sessionfactory.opensession (); Transaction TX=session.begintransaction (); Session.save (PO); Tx.commit (); Session.close (); } Public voidCloseSession ()throwshibernateexception {sessionfactory.close (); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.