reto
reto

Reputation: 16752

Simplest way of using Hibernate Sessions

I'm coaching a student project which uses hibernate as a persistence layer. From my projects at work I'm already quite familiar with hibernate and can use it with few 'troubles'. But with this project we have constantly problem with sessions, stale objects and and 'object was loaded from a different session'-errors.

So what I am looking for the simplest possible way to use sessions:

Ideal would be:

Its a single-process GUI application. The current setting for current_session_context_class is thread. But I use a static field for the session variable (which I think causes some of my troubles) and only fetch it ONCE.

Thanks for your assistance!

Cheers, Reto

Upvotes: 1

Views: 287

Answers (1)

Affe
Affe

Reputation: 47994

Assuming you're not teaching ORM, the understanding of why these errors happen isn't part of the knowledge the students are supposed to come away with etc etc etc and you just want Hibernate to work as a database wrapper so they can get data to use while learning other things.

This is probably your best bet:

StatelessSession session = sessionFactory.openStatelessSession();

A stateless session is effectively "auto-commit mode for ORM" and as close to raw-JDBC wrapper as hibernate gets. No Sessions, no L1 caches, no persistence context. Just SQL/HQL that returns objects.

Upvotes: 2

Related Questions