Results 1 to 8 of 8

Thread: Spring+Hibernate in a layered architecture

  1. #1
    Join Date
    Aug 2004
    Location
    A Coruña, Spain
    Posts
    17

    Default Spring+Hibernate in a layered architecture

    Hi,

    I use Hibernate and I'm having trouble with persistent objects and transactions.

    In my business layer I call my DAO methods (e.g. load) that in turn call HibernateTemplate.load and return the persisted object to the business layer.
    There I want use it as it were a Transfer Object, changing some of its values, but as it's done inside a transaction the object is cached in the session and every change I made to it is persisted when closing the session at the end of the transaction (are these assumptions true?)
    Seems that there's no method to dettach persisted objects from the session and maybe a solution could be return a clone of the persisted object in the DAO or creating a Transfer Object with data from the persisted object.

    If anyone asks about it I don't want to use the OpenSessionInView filter as it breaks the layered architecture.

    And a sample can be a use case where I want to return a department with only the employees that have some attribute. I load the department object with a lazy collection of employees calling dao.load(). Then I can call dao.getEmployeesXXX that uses a filter to get only a subset of employees from the lazy collection).
    To sum up:
    Code:
    department = dao.load(id);
    department.setEmployees(dao.getEmployeesXXX(department));
    if department is the same object as returned by Hibernate then I'll get errors or it will be update in database with only those employees from dao.getEmployeesXXX

    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    With Hibernate3 you could use Filters :wink:

    I think you can do the following:
    Code:
      //add a copy contructor to your Department class
      public Department(Department department) {
        //copy base properties
      }
    
      //then in your DAO
      department = dao.load(id); 
      Department dept = new Department(department );
      dept.setEmployees(dao.getEmployeesXXX(department));
    If you do not like the copy constructor (as in normal use, it should also copy the employees), you can copy a subset properties with d1.setXXX(d2.getXXX()) in your dao.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    A Coruña, Spain
    Posts
    17

    Default

    Yes I have read something about Hibernate3, but I can't wait :cry:

    The copy constructor is almost the same option as cloning, I think that's what I'll use if there's no better option, but a better approach than your implementation can be using commons-beanutils reflection methods.

    I think somebody else has to have faced this problem because one of the main reasons to use ORM is not to code TOs

  4. #4
    Join Date
    Aug 2004
    Posts
    16

    Default

    Hi Carlos,

    Let me make sure about your question and then I'll give it a shot. You want to load an object (I'll use your Department/Employees examples), filter it's collections and then return it to your view. You are concerned that by filtering the collection the Employees removed by the filter will lose their association to the Department when the session is flushed (ie when the transaction commits).

    If this is the case you want to mark your transaction as readOnly. When using hibernate this means the session will not be flushed. There was just recently a thread on this here,

    http://forum.springframework.org/showthread.php?t=9675

    Hope this is helpful,

    Mike
    Last edited by Rod Johnson; Jan 18th, 2006 at 10:10 AM.

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    but a better approach than your implementation can be using commons-beanutils reflection methods.
    Agree, however I think making my domain object dependent on commons-beanutils would not be a good design.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  6. #6
    Join Date
    Aug 2004
    Location
    A Coruña, Spain
    Posts
    17

    Default

    Quote Originally Posted by futang
    You are concerned that by filtering the collection the Employees removed by the filter will lose their association to the Department when the session is flushed (ie when the transaction commits).
    You're right

    Quote Originally Posted by futang
    If this is the case you want to mark your transaction as readOnly.
    This is not possible some times, e.g. I want the createDepartment method to return the created object, so in that transaction I want to both update database and transform an hibernate persisted object in a TO.

  7. #7
    Join Date
    Aug 2004
    Posts
    3

    Default

    Would a session.evict(Object) accomplish what you want? It detaches the instance from the currently open session.
    -wd

  8. #8
    Join Date
    Aug 2004
    Location
    A Coruña, Spain
    Posts
    17

    Default

    But also prevents flushing wanted changes to database

Similar Threads

  1. Replies: 5
    Last Post: Feb 3rd, 2009, 05:19 AM
  2. Replies: 3
    Last Post: Aug 16th, 2007, 12:10 PM
  3. Replies: 5
    Last Post: Mar 14th, 2005, 10:18 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •