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;
}

Tags:

Monday, July 20th, 2009 Code, Database, Development

2 Comments to Hibernate StatelessSession with Transactional Annotation

  1. 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

  2. shagzy on September 25th, 2009
  3. 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.

  4. Andrew Eells on October 17th, 2009

Leave a comment

You must be logged in to post a comment.

Tech Startup