Consider a condition when you need to upload a large number of records into your database using Hibernate. Following is the code clip to reach this using Hibernate:
Since by default, Hibernate will cache all the persisted objects in the session-level cache and finally your application would fall over with an OutOfMemoryException somewhere around the 50,000th row. You can resolve this predicament if you are using batch processing with Hibernate.
To use the group processing feature, first set hibernate.jdbc.batch_size as batch size to a numeral either at 20 or 50 depending on object size.
This will tell the hibernate container that every X rows to be inserted as batch. To apply this in your code we would need to do small alteration as follows:
Above code will work well for the INSERT operation, but if you are willing to create UPDATE operation then you can complete using the following code:
Let us change configuration file as to add hibernate.jdbc.batch_size property:
Consider the following POJO Employee class:
Let us make the following EMPLOYEE table to accumulate Employee objects:
Following will be mapping case to map Employee substance with EMPLOYEE table.
At last, we will create our application class with the main () method to run the application where we will use flush () and clear () methods accessible with Session object so that Hibernate keep script these records into the database in its place of caching them in the memory.
Here are the steps to compile and run the above mentioned application. Construct sure you have set PATH and CLASSPATH exactly before arranged for the compilation and implementation.
Hibernate Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JSP Interview Questions||Java-Springs Interview Questions|
|Java Struts Interview Questions||Log4j Interview Questions|
|Software Development Lifecycle (SDLC) Interview Questions||NHibernate Interview Questions|
|Spring MVC Framework Interview Questions||Apache Struts 2 Interview Questions|
|Spring Aop Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.