Posts

Hibernate Interceptor Lesson

We will use following files Entity.java Entity.hbm.xml hibernate.cfg.xml MyInterceptor.java HibernateEngine.java Entity.java public class Entity{ private int id; String name; String category; } Entity.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="javafiles.Entity" table="ENTITIES_DATA"> <id name="id" column="ID" > <generator class="assigned" /> </id> <property name="name" column="NAME" length="20"/> <property name="category" column="CATEGORY" length="20"/> </class> </hibernate-mapping> MyInterceptor.java package javafiles; import java.io.Serializable; import java.util.Iterator; import org.hibernate.CallbackE...

Hibernate Interceptor

Interceptor Interface provides methods which can be called at different stages to perform some required tasks. Interceptor 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. Hibernate Interceptor gives us total control over how an object will look to both the application and the database. To build/use an interceptor we can either implement Interceptor interface directly or extend EmptyInterceptor class . Following are some of the methods available within the Interceptor interface: Method and Description findDirty() This method is be called when the flush() method is called on a Session object. instantiate() This method is called when a persisted class is instantiated. isUnsaved() This method is called when an object is passed to the saveOrUpdate() method onDelete() This method is called before an object is deleted. When object is not...

Hibernate Batch Processing Insert Operation

We will use following files Student.java Student.hbm.xml hibernate.cfg.xml HibernateEngine.java Student.java public class Student { private int rollNumber; private String name; private String address; // set and get method } Student.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="javafiles.Student" table="STUDENTS_DATA"> <id name="rollNumber" column="ROLL_NUMBER" > <generator class="assigned" /> </id> <property name="name" column="NAME" length="20"/> <property name="address" /> </class> </hibernate-mapping> Hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-config...

Batch Processing

Friends, batch processing means processing insert ,udate and delete operations in batch or bulk, Just for the sake of our understanding lets take a small example, If we want to insert 100000 records into the database then our code will be as below Session sessionObj = sessionFactoryObj.openSession(); Transaction transactionObj = sessionObj.beginTransaction(); for ( int y=0; y<100000; y++ ) { House houseObj = new House(……); session.save(houseObj); } transactionObj.commit(); sessionObj.close(); Friends what do you think , will it run successfully ?? No ! This will fail with an OutOfMemoryException somewhere around the 50,000th row. That is because Hibernate caches all the newly inserted Customer instances in the session-level cache. To overcome this problem If you are undertaking batch processing you will need to enable the use of JDBC batching. This is absolutely essential if you want to achieve optimal performance. Set the JDBC batch size to a appropriate number ...

Hibernate One to One Update operation

// For pojo see the One to One Insert operation import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateEngine { public static void main(String[] javaPlanet) { System.out.println(" ....... ENGINE START ............"); System.out.println(" ....... ONE TO ONE ANNOTATION UPDATE LESSON ............\n"); Configuration configurationObj = new Configuration(); configurationObj.configure("hibernate.cfg.xml"); SessionFactory sessionFactoryObj = configurationObj.buildSessionFactory(); Session sessionObj = sessionFactoryObj.openSession(); Result resultObj = (Result) sessionObj.get(Result.class, new Integer(4)); Pupil pupilObj= resultObj.getPupil(); pupilObj.setName("Mansukh"); Transaction transaction=sessionObj.beginTransaction(); sessionObj.update(resultObj); transaction.commit(); System.out.println("\n....... DATA UPDATED SUCCESSFULLY ....

Hibernate One To One Insert Operation

We will use following files Pupil.java Result.java hibernate.cfg.xml HibernateEngineInsert.java Pupil.java @Entity @Table(name="PUPILS_DATA") public class Pupil{ @Id @Column(name="ROLL_NUMBER") int rollNumber; @Column(name="NAME") String name; @Column(name="ADDRESS") String address; @OneToOne(targetEntity=Result.class,cascade=CascadeType.ALL) @JoinColumn(name="ROLL_NUMBER",referencedColumnName="ROLL_NUMBER") Result result; // set and get methods } Result.java @Entity @Table(name="PUPILS_RESULTS_DATA") public class Result { @Id @Column(name="ROLL_NUMBER") private int rollNumber; @Column(name="MATHS_MARKS") private int mathsMarks; @Column(name="SCIENCE_MARKS") private int scienceMarks; @Column(name="TOTAL_MARKS") private int totalMarks; @OneToOne(targetEntity=Pupil.class,cascade=CascadeType.ALL) @JoinColumn(name="ROLL_NUMBER",referencedColumnNa...

Hibernate Many to Many Update Operation

// For SQL and POJO see the Many to Many Insert Operation import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateEngine { public static void main(String[] javaPlanet) { System.out.println(" ....... ENGINE START ............"); System.out.println(" .. MANY TO MANY ANNOTATION UPDATE LESSON ..\n"); Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); User userObj = (User) session.get(User.class, new Integer(2)); userObj.setName("Dharm"); Set<MobilePhone> mobilePhoneSet = userObj.getMobilePhones(); for(MobilePhone phone:mobilePhoneSet){ if(phone.getModelNumber()==11)     phone.setName("SAMSUNG galaxy NOTE"); } userObj.setMobilePhones(mobilePhoneSet); Transaction...