Announcement Announcement Module
Collapse
No announcement yet.
BlazeDS & Hibernate lazy loading in n-tier arhitecture Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BlazeDS & Hibernate lazy loading in n-tier arhitecture

    Hi,

    We have faced an issue on lazy loading Domain Objects with Hibernate in n-tier architecture with Spring BlazeDS.

    Our architecture is just a basic Spring architecture model:

    - Flex Client

    - Web layer (includes BlazeDS configuration)
    - Service layer
    - HIbernateDAO+Domain model

    For example, if we want to load this domain object

    Code:
    // example...
    public class Customer {
      private List<Order> orders;
      private Address address
      // getters & setters
    }

    Orders and Address -fields are marked with lazy fetch. So when we simply load Customer, orders aren`t loaded at the same time, they are just Hibernate proxies. Hibernate session is closed after DAO layer.

    Service layer gets the Customer object from DAO layer. Flex client requests the service layer method with some parameters trough BlazeDS and service layer returns the Customer to Flex.

    And now comes the problem: lazyInitException.

    After some searching and googling we founded out this: BlazeDS doesn`t recognize lazy fields (Hibernate proxies) and gives the error.

    Possible solutions were:
    a) change your architecture so, that you can use Open Session View Pattern
    b) mark all lazy fields (Hibernate proxies) to null on service layer
    c) use the Data Transfer Object anti-pattern: do not return any domain object to Flex, create matching DTO`s (for example CustomerDTO) and transform your domain objects to DTO on service layer.

    We used solution c.

    Discussion about this, please. Tell you experiences on lazy loading and solution you have used.
    Last edited by Durden; May 9th, 2009, 10:09 AM. Reason: Cleared out things of Hibernate proxies

  • #2
    Have you checked out this project, might be helpful
    http://noon.gilead.free.fr/gilead/

    Comment


    • #3
      We used DTO as well with the excellent project Dozer. Gilead looks promising though.

      Comment


      • #4
        We disabled lazy loading in hibernate. I am not so happy that we had to do that, but it works so far.

        Comment


        • #5
          Solution C

          Hi,


          Can you explain more on solution C, did you use any common methods to convert your Domain objects to DTO . I am new to this technology, so any help will be helpful.


          I also read somewhere, that you can have a shorter version of Customer class, which will not have the Order property , so where ever you don't want to load all the related info then use this shorter Domain class. But I know this duplicates.

          But still I am trying to find a more simple way, because I feel that, we are using lot of technology(flex,blazeds,spring, hibernate) to do a simple job. Or may be, is it only I, who thinks so



          Thank you .

          Kannan

          Comment


          • #6
            Originally posted by kannanMugundan View Post
            Hi,


            Can you explain more on solution C, did you use any common methods to convert your Domain objects to DTO . I am new to this technology, so any help will be helpful.


            I also read somewhere, that you can have a shorter version of Customer class, which will not have the Order property , so where ever you don't want to load all the related info then use this shorter Domain class. But I know this duplicates.

            But still I am trying to find a more simple way, because I feel that, we are using lot of technology(flex,blazeds,spring, hibernate) to do a simple job. Or may be, is it only I, who thinks so



            Thank you .

            Kannan
            I used the Mapper/Assembler -pattern to map my Domain objects into DTO`s on Service layer. My DAO`s are returning Domain objects into Service layer, which converts Domain objects into DTO`s using Assembler.

            See:

            http://java.sun.com/blueprints/corej...ferObject.html

            http://martinfowler.com/eaaCatalog/d...ferObject.html

            Comment


            • #7
              Durden, can u post an example, about how to use this pattern?
              For a classical example users-groups or person-company, where have relation ManyToOne and OnetoMany.
              I have problem with lazy loading, i got an error about CGLIB Enhancement failed.
              I think that many users have same problem, so i think that can be very useful for all users, an clear example.

              Comment


              • #8
                Originally posted by vlad2005 View Post
                Durden, can u post an example, about how to use this pattern?
                For a classical example users-groups or person-company, where have relation ManyToOne and OnetoMany.
                I have problem with lazy loading, i got an error about CGLIB Enhancement failed.
                I think that many users have same problem, so i think that can be very useful for all users, an clear example.

                Here`s some quick and dirty meta code for getting the idea. Just create matching classes (Domain & DTO). Return domain object from DAO level and map it to DTO with custom mapper.

                Note: this is just one possible (a bit ugly) solution!

                Code:
                public class PersonDTO {
                
                 private String name; 
                 // getters & setters etc
                }
                Code:
                public class CompanyDTO {
                 private String companyName; 
                 private List<PersonDTO> persons;
                 // getters & setters, adding methods etc
                }
                Code:
                public class CompanyMapper {
                
                  public static CompanyDTO mapCompanyDTOWithPersons(Company company) {
                       CompanyDTO dto = new CompanyDTO
                       dto.setCompanyName = company.getName
                       // etc....
                
                      List<Person> persons = company.getPersons;
                       PersonDTO personDTO = null;
                     foreach(Person person in persons) {
                       personDTO = new PersonDTO();
                        personDTO.setName(person.getName);
                        // etc...
                        companyDTO.addPersonDTO(personDTO)
                     }
                  return companyDTO;
                  }
                
                }

                Comment


                • #9
                  Also used the DTO and Dozer why would you want to expose your hibernate domain classes everywhere in our SOA efforts that would lead to a very weak architecture.

                  Comment


                  • #10
                    Will there be out of box soultion

                    I really don't want to use DTO, IMHO it won’t encourage some one who is lazy to take up this technology, and it is a double work, for every domain object creating DTO. When we have deadline to finish the project, we need to go for some plug-in or use DTO to avoid this problem, which makes the learning curve steep and time consuming. I really hope spring and Adobe come up with some out of box solution for this lazy loading issue, so it would make this technology as one of the best for RIA. If some one from Spring is reading this, Please let us know if there is any plan to come up with an out of box solution. We will be getting more projects in days to come and I would really like to use SpringBlazeDs provided with a fix for this problem.

                    Comment


                    • #11
                      Originally posted by kannanMugundan View Post
                      I really don't want to use DTO, IMHO it won’t encourage some one who is lazy to take up this technology, and it is a double work, for every domain object creating DTO. When we have deadline to finish the project, we need to go for some plug-in or use DTO to avoid this problem, which makes the learning curve steep and time consuming. I really hope spring and Adobe come up with some out of box solution for this lazy loading issue, so it would make this technology as one of the best for RIA. If some one from Spring is reading this, Please let us know if there is any plan to come up with an out of box solution. We will be getting more projects in days to come and I would really like to use SpringBlazeDs provided with a fix for this problem.
                      See the up-coming Hibernate serialization support: http://static.springsource.org/sprin...ng-blazeds.pdf

                      Comment


                      • #12
                        Hibernate serialization support

                        Regarding the following upcoming feature, do we have any dates?
                        Hibernate serialization support
                        Make it easier to serialize Hibernate entities to AMF without
                        LazyInitializationException, etc.

                        Comment


                        • #13
                          Spring BlazeDS with dpHibernate ?

                          +1

                          At site url, http://static.springsource.org/spring-flex/ we can see that there is file named "dpHibernate-test.zip" so...

                          Can't wait to get release date.

                          Comment


                          • #14
                            The Hibernate support will be part of the 1.5 release which we hope to have out by the end of the year.

                            Comment


                            • #15
                              Originally posted by jeremyg484 View Post
                              The Hibernate support will be part of the 1.5 release which we hope to have out by the end of the year.
                              Excellent news! Thanks for good job.

                              Comment

                              Working...
                              X