This was a good blogpost to understand stateless batching. Here are excerpts from the post.

We already know, in batch insertion, you only flush the session after intervals of 50 or 100 so that the memory burden of clearing the session and managing the session cache is mitigates and whatismore, it allow hibernate to batch 50-100 inserts together.

What stateless session batching does is something else, much closer to JDBC

StatelessSession class differs from Session class in that it does not cache the objects, does not call interceptors, does not save any persistence context of object, does not cascade to composed objects, does not take care of collections, directly transfers the object to jdbc insert statement. With StatelessSession, you have to save composed objects separately e.g. in above example, you have to insert publisher and books separately. Above code with StatelessSession will be:


StatelessSession session = HibernateUtil.getSessionFacgtory().openStatelessSession(); session.beginTransaction();
for (int index = 0; index < style="font-style: italic; color: rgb(255, 153, 102);"> {
Book book = new Book();

book.setAuthor("amer");
book.setIsbn("34343");

book.setName("Hibernate " + index);
Publisher pub = new Publisher();
pub.setName("Publisher " + index);
book.setPublisher(pub);

book.setPublishDate(new Date());
session.insert(pub);

session.insert(book);
}
session.getTransaction().commit();

session.close();

In a stateful session, you could simply do

session.save(book);

Stateless session is not recommended for composed objects (e.g Publisher and Book)


This free site is ad-supported. Learn more