Results 1 to 4 of 4

Thread: One-to-Many Data Retrieval issue when using Hibernate

  1. #1
    Join Date
    Nov 2004
    Location
    Saint Louis, MO USA
    Posts
    9

    Default One-to-Many Data Retrieval issue when using Hibernate

    Hi,

    I have an issue while retrieving the data from the database using Hibernate.
    This is a very simple structure. A Person object having many Address objects.

    Code:
    Person {
        Long personId;
        String Name;
        String emailAddress;
        List addresses;
    }
    Address {
        Long personId;
        Long postalId;
        String addressLine1;
    }
    In my hbm.xml file I have stated inverse="true" and lazy="true"

    In my PersonDAO implementation

    I have a method called

    Code:
    public Person getPerson(String pEmailAddress) throws DataAccessException {
        final String query = "from webstore.domain.Person as person where " +
          "email_address = ?";
        Object param = new Object();
        param = pEmailAddress;
    
        List tmpList = getHibernateTemplate().find(query, param);
    
        if &#40;tmpList.size&#40;&#41; < 1&#41; &#123;
          return null;
        &#125;
    
        Iterator itrOuter = tmpList.iterator&#40;&#41;;
        LOGGER.info&#40;"Size of the list retrieved ="+tmpList.size&#40;&#41;&#41;;
        while &#40;itrOuter.hasNext&#40;&#41;&#41; &#123;
            Person tmpParty =&#40;&#40;Person&#41;itrOuter.next&#40;&#41;&#41;;
            LOGGER.info&#40;"Person First Name is "+tmpParty.getFirstName&#40;&#41;&#41;;
            List addressList = tmpParty.getAddresses&#40;&#41;;
            LOGGER.info&#40;"Size of the address object ="+addressList.size&#40;&#41;&#41;;
            Iterator itrInner = addressList.iterator&#40;&#41;;
            while&#40;itrInner.hasNext&#40;&#41;&#41; &#123;
                PostalAddress postAddress = &#40;PostalAddress&#41;itrInner.next&#40;&#41;;
                LOGGER.info&#40;"Address Line1 ="+postAddress.getAddressLine1&#40;&#41;&#41;;
            &#125;
        &#125;
    
        return &#40;Person&#41; tmpList.get&#40;0&#41;;
      &#125;
    If you look at the code I intentionally added log.info to print the size of the addressList.
    Even though for the test data that I have has only one address record, The above mention statement tells me there are 2 objects in the collection. When I did further investigation the first object is null and the second object has the right values.

    Now my question is how/where does the null object gets added to the collection?

    Is this a Hibernate issue or some configuration issue.

    Pls help.

    Thanks,
    Arun
    Arun Viswanathan
    Dreamsoft Solutions, Inc.

  2. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    Arun,

    This sounds like an issue with Hibernate so you should probably post this on their support board. However, before you do, you should try turning on Hibernate debugging and finding the SQL commands that are accessed and running them separately to check that this isn't a problem with your data.

    Rob

  3. #3
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    Are you sure that is the correct query string?

    final String query = "from webstore.domain.Person as person where " +
    "email_address = ?";
    Shouldn't it be
    Code:
    final String query = "from webstore.domain.Person as person where " + 
          "person.emailAddress = ?";
    ?
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  4. #4
    Join Date
    Nov 2004
    Location
    Saint Louis, MO USA
    Posts
    9

    Default

    final String query = "from webstore.domain.Person as person where " +
    "email_address = ?";

    final String query = "from webstore.domain.Person as person where " +
    "person.emailAddress = ?";
    Both the queries have the same effect.

    But this is what I found from Hibernate forums.

    Using List implementation for collections is not a recommended option. So I reimplemented it using Set. The problem went away.

    In one of the topic they have discussed the option of using the List. I tried, but it did not work for me. I believe it is something to do with the hbm.xml file.

    http://forum.hibernate.org/viewtopic...1ecfeac201aac9

    http://www.hibernate.org/193.html

    Arun
    Arun Viswanathan
    Dreamsoft Solutions, Inc.

Similar Threads

  1. Replies: 7
    Last Post: Apr 18th, 2007, 09:50 AM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Multiple Data Sources + Hibernate + Spring
    By joeserel in forum Data
    Replies: 2
    Last Post: May 18th, 2005, 09:22 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 1
    Last Post: Oct 20th, 2004, 05:40 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
  •