Skip to main content

Posts

Showing posts with the label Database

ORM Overview

What is JDBC? JDBC stands for  Java Database Connectivity  and provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database. JDBC provides a flexible architecture to write a database independent application that can run on different platforms and interact with different DBMS without any modification. Pros and Cons of JDBC Pros of JDBC Cons of JDBC Clean and simple SQL processing Good performance with large data Very good for small applications Simple syntax so easy to learn Complex if it is used in large projects Large programming overhead No encapsulation Hard to implement MVC concept Query is DBMS specific Why Object Relational Mapping (ORM)? When we work with an object-oriented systems, there's a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as J...

Hibernate Batch Processing

Consider a situation when you need to upload a large number of records into your database using Hibernate. Following is the code snippet to achieve this using Hibernate: Session session = SessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Employee employee = new Employee(.....); session.save(employee); } tx.commit(); session.close(); Because by default, Hibernate will cache all the persisted objects in the session-level cache and ultimately your application would fall over with an  OutOfMemoryException  somewhere around the 50,000th row. You can resolve this problem if you are using  batch processing  with Hibernate. To use the batch processing feature, first set  hibernate.jdbc.batch_size  as batch size to a number 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 implement this in your code we would need to do...

Hibernate Interceptors

As you have learnt that in Hibernate, an object will be created and persisted. Once the object has been changed, it must be saved back to the database. This process continues until the next time the object is needed, and it will be loaded from the persistent store. Thus an object passes through different stages in its life cycle and  Interceptor Interface  provides methods which can be called at different stages to perform some required tasks. These methods are callbacks from the session to the application, allowing the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. Following is the list of all the methods available within the Interceptor interface: S.N. Method and Description 1 findDirty() This method is be called when the  flush()  method is called on a Session object. 2 instantiate() This method is called when a persisted class is instantiated. 3 isUnsaved() This method is called when an o...

Hibernate Caching

Caching is all about application performance optimization and it sits between your application and the database to avoid the number of database hits as many as possible to give a better performance for performance critical applications. Caching is important to Hibernate as well which utilizes a multilevel caching schemes as explained below: First-level cache: The first-level cache is the Session cache and is a mandatory cache through which all requests must pass. The Session object keeps an object under its own power before committing it to the database. If you issue multiple updates to an object, Hibernate tries to delay doing the update as long as possible to reduce the number of update SQL statements issued. If you close the session, all the objects being cached are lost and either persisted or updated in the database. Second-level cache: Second level cache is an optional cache and first-level cache will always be consulted before any attempt is made to locate an object in the secon...

Hibernate Criteria Queries

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions. The Hibernate  Session  interface provides  createCriteria()  method which can be used to create a Criteria  object that returns instances of the persistence object's class when your application executes a criteria query. Following is the simplest example of a criteria query is one which will simply return every object that corresponds to the Employee class. Criteria cr = session.createCriteria(Employee.class); List results = cr.list(); Restrictions with Criteria: You can use  add()  method available for  Criteria  object to add restriction for a criteria query. Following is the example to add a restriction to return the records with salary is equal to 2000: Criteria cr = session...