Announcement Announcement Module
No announcement yet.
Service layer design issue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Service layer design issue

    Hey all, relatively new to Spring/Hibernate and have the following issue:

    Just curious about the proper way to do the following in my app. I have a couple methods on my facade that allow me to a.) load an object, and then subsequently b.) find an associated object for that particular object.

    For example

    Game loadGame( int id );
    GameImage getDefaultImage( Game game )
    The problem with doing this is that a new session is created when the getDefaultImage method is called. Hibernate throws the exception:
    net.sf.hibernate.QueryException: The collection was unreferenced.
    This is occurring because the game that is being "filtered" in Hibernate was loaded in the session for the loadGame method.

    The filter code in getDefaultImage does the following:

    Query q = session.createFilter( game.getImages(), "order by this.position" );
    q.setMaxResults( 1 );
    So, my question is, what is the proper way to handle this? The only solution I can think of is to load the game + the default image and return a holder type object.

    I didn't have the issue prior to using Spring because I used a single Session per thread, etc.

    Any help would be greatly appreciated.



  • #2

    You can continue to use a single Session per thread with Spring. My preferred way of doing this is to use transactional proxies on my business tier objects. This will ensure that all calls into your data tier use the same transaction and session. You can optimize for read-only operations by marking transactions as read-only.

    Alternatively, you can use the OpenSessionInViewInterceptor or OpenSessionInViewFilter to open a session at the beginning of your web request.

    See chapter 7 of the reference manual for more information on transaction management. Also see the sample apps from practical examples of transaction management uses.



    • #3

      Thanks for the info. I should have mentioned, I currently work around it using the single session in view filter w/ singleSession set to true. The problem of course occurs, when singleSession is set to false. It also occurs in my test cases.

      I will look more into using transactional proxies to allow for a single session/transaction. Currently I am just using the TransactionProxyFactoryBean for my facade.