Hibernate implements the Insert self-increment ID method after the table is divided.

Source: Internet
Author: User

#第一种基于原生的JDBC的方式实现

Private Integer Insert (POJO entity, string table) throws SQLException {string insertsql = "INSERT into" + table + "(XXXX ) VALUES (?,?,?,?,?,?,?,?) ";D Atasource DataSource = Sessionfactoryutils.getdatasource (Gethibernatetemplate (). Getsessionfactory ()); Connection Connection = null; PreparedStatement pstatement = null; ResultSet ResultSet = null;try {connection = datasource.getconnection ();p statement = Connection.preparestatement ( Insertsql, Statement.return_generated_keys);p statement.setint (1, Entity.getsid ());p Statement.setint (2, Entity.getnum ());p statement.executeupdate (); resultSet = Pstatement.getgeneratedkeys (); if (Resultset.next ()) { return Resultset.getint (1);}} finally {if (ResultSet! = null) {resultset.close (); resultSet = null;} if (pstatement! = null) {pstatement.close ();p statement = null;} if (connection! = null) {connection.close (); connection = null;}} return-1;}


#第二种 using native-based hibernate implementations

Import Java.util.date;import Org.hibernate.session;import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.orm.hibernate3.hibernatetemplate;import org.springframework.stereotype.component;@ Componentpublic class Testdao {@Autowired @qualifier ("xxxxtemplate") Private hibernatetemplate Template;private Sqlinterceptor Interceptor = new Sqlinterceptor ("TableName");p ublic void Save (POJO entity) {Session session = Template.getsessionfactory (). Opensession (Interceptor);/* Split table policy: Split data by month */integer Yearmonth = Integer.parseint ( Dateutils.format (New Date (), "yyyymm")), Interceptor.setnewtable ("Tablename_" +yearmonth); Session.save (entity); Session.close (); System.out.println ("Return primary Key ID:" + entity.getid ());}}
#HibernateSQL拦截器

Import Org.hibernate.emptyinterceptor;public class Sqlinterceptor extends Emptyinterceptor {private static final long Serialversionuid = 1637672155224242981l;public sqlinterceptor (String table) {this.table = table;} public string Table;private string newtable;public void Setnewtable (String newtable) {this.newtable = newtable;} @Overridepublic string onpreparestatement (String sql) {if (Stringutils.isnotempty (table, newtable)) {sql = Sql.replaceall (table, newtable);} return super.onpreparestatement (SQL);}}




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.