Hibernate StatelessSession with Transactional Annotation
I have a bi-directional collection mapping in Java which I thought was causing me problems when persisting using Hibernate’s StatelessSession API.
However, it looks as though the issue was being caused by the fact I was using the @Transactional annotation instead. The API states:
“Operations performed via a stateless session bypass Hibernate’s event model and interceptors.”
I didn’t pick up on the importance of the above statement as it’s not particularly clear at first glance, but it looks as though using Annotations via AOP just isn’t supported when streaming directly to the database using the StatelessSession.
Commenting out the annotation and using a programmatic Transaction instead corrected the problem:
// @Transactional
public List<Package> insertAvailability(final List<Package> packages) {
final StatelessSession session = sessionFactory.openStatelessSession();
final Transaction tx = session.beginTransaction();
session.insert(aPackage);
// insert collections manually etc...
tx.commit();
session.close();
return packages;
}
2 Comments to Hibernate StatelessSession with Transactional Annotation
Leave a comment
You must be logged in to post a comment.
Blogroll
Tech Startup
- "I don't want to be a product of my environment. I want my environment to be a product of me." - Frank Costigan, The Departed 1 week ago
- Actually said 'Good Morning' to his Mac when it it announced 'It's 8 o'clock'. Taking it to a whole new level... 2 weeks ago
- @auxbuss have found mockflow more helpful lately, partly as it's integrated with google apps. balsamiq has a more aesthetic UI though ;o) 2 weeks ago
- is reading up on #NoSQL and how best to store several '000 small read-only data tables. answers on a postcard please ;o) 2 weeks ago
- is now a big big fan of #git still getting there with the command line though, latest learning is git log --name-status 3 weeks ago
- @lleighton something i can help with? in reply to lleighton 2010-08-08
- has broken the @Oracle database. that's a first ;o) 2010-07-17
- how to extend base or framework test classes from another module with #maven http://bit.ly/5hSvg 2010-06-24
- #java #developers. Interested in an #agile financial services #startup with secured funding? http://bit.ly/a0NNiX 2010-06-16
- looking into #oracle 11g UCP 2010-05-27
- More updates...

hi,
thanks for the article. it was very useful.
I wanted to know what exactly you meant by comment “// insert collections manually”. I am using stateless session which ignores the collectionm map in my object while saving the object. What is the work around for it?
Kindly let me know.
Thanks
hi, sorry for not replying sooner.
i think all you have to do is call session.insert() for each item in your collection, assuming your foreign key dependency has been set up by the initial insert() statement.
this code has long since disappeared having refactored the above process, but if i remember correctly it should be fairly straightforward.
let me know if you have any further problems if you haven’t already solved this yourself.