Announcement Announcement Module
Collapse
No announcement yet.
One-to-Many Data Retrieval issue when using Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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

    Comment


    • #3
      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 = ?";
      ?

      Comment


      • #4
        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

        Comment

        Working...
        X