Hibernate bulk Operation data

Source: Internet
Author: User

Bulk processing of data

① Batch operation via session

? Specifically, after processing an object or a small batch object, immediately call the flush method to clean up the cache, and then call the clear method to empty the cache

<!--set the number of JDBC single batch processing--

<property name= "Hibernate.jdbc.batch_size" >20</property>

Hibernate cannot bulk insert at the JDBC layer if the object uses the identity identifier generator

It is recommended to turn off level two cache for bulk operations!!!

<!--turn off level two cache--

<property name= "Hibernate.cache.user_second_level_cache" >false</property>

② calling stored procedures using CallableStatement

1. Stored procedures for inserting data:

Create or Replace procedure Login_insert (username in Varchar,password with varchar,age in number) as

Begin

Insert into login (username,password,age) values (username,password,age);

End;

2. Stored procedures for updating data:

Create or Replace procedure login_update (a in number) as

Begin

Update Lobin set age=a where username= ' Tom ';

End;

3. Stored procedures for deleting data:

Create or Replace procedure login_delete (uname in varchar) as

Begin

Delete form login where USERNAME = uname;

End;

Calling a stored procedure that inserts data

Session session = new Configuration (). Configure (). Buildsessionfactory (). Opensessin ();

Transaction Tran =session.begintransaction ();

CallableStatement CST = Session.connection.prepareCall ("{Call Login_insert (?,?,?)}");

Cst.setstring (1, "AAA");

Cst.setstring (2, "22222");

Cst.setlong (3,21);

Cst.executeupdate ();

Tran.commit ();

Session.close ();

Stored procedures for querying data:

*logins represents a system cursor that assigns the query result to logins.

Create or Replace procedure login_getlist (logins out sys_refcursor)

Begin

Open logins for SELECT * from login;

End;

? How to call this stored procedure with an output parameter (return result set) in Hibernate:

Call a stored procedure with a return result set

Session session = new Configuration (). Configure (). Buildsessionfactory (). Opensession ();

Transaction tran = Session.begintransaction ();

CallableStatement CST = Session.connection (). Preparecall ("{Call Login_getlist (?)}");

/* Call CallableStatement's Registeroutparameter () method, pass the parameter, "1" represents the first question mark, the second parameter is the Oracle data type, because the stored procedure is the result set returned with the output parameters of the system cursor type, So here's the type of Oracle cursor to pass in. */

Cst.registeroutparameter (1,oracle.jdbc.oracletypes.cursor);

Cst.execute ();

ResultSet rs = (ResultSet) cst.getobject (1);//Gets the output parameters of the stored procedure, that is, the output parameters of the system cursor, to use coercion type conversion

while (Rs.next ()) {

System.out.print ("User name:" +rs.getstring ("username"));

System.out.print ("Password:" +rs.getstring ("password"));

System.out.print ("Ages:" +rs.getstring ("Age"));

}

Tran.commit ();

Session.close ();

To call a stored procedure using named sql:

<!--call the stored procedure of the query, callable= "True" represents the naming of our claims support SQL stored procedure, cannot be omitted, omit will error--

<sql-query name= "Logingetlist" callable= "true" >

{call Login_getlist (?)}

<return alias= "1" class= "Com.test.Login"/>

</sql-query>

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.