Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4079804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:50:55+00:00 2026-05-20T17:50:55+00:00

I’ve setup a one-to-many/many-to-one bi-directional mapping relationship for a User class and a Pet

  • 0

I’ve setup a “one-to-many”/”many-to-one” bi-directional mapping relationship for a User class and a Pet class (for a pet store-themed academic web project). Each User owns collection of pets.

It seems the relationship is working fine, because when I execute a query to select the first User in my database in NetBeans’ HQL Query Browser, I see that the “Pets” attribute has 2 Java object ID’s listed, presumably the two pets I referenced via the foreign key ( [HibernateBeans.Pet@76838e, HibernateBeans.Pet@b71950] ).

Frustratingly (and incorrectly), my code below returns an empty collection when I pass in the exact same user that I successfully queried for:

public ArrayList<Pet> getPets(User user) {
    Set<Pet> pets = null;
    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        pets = user.getPets();
        for (Pet p : pets) System.out.println(p.getPetName());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return pets;
}

EDIT: As per Tahir’s suggestions, I’ve removed the annotations from my Java classes and am now strictly using POJO XML mapping (but the same problem persists). My source code below has been updated accordingly:

Here are the relevant XML Hibernate mappings:

User.hbm.xml:

<set name="pets" inverse="true">
    <key>
        <column name="UserID" not-null="true" />
    </key>
    <one-to-many class="HibernateBeans.Pet"/>
</set>

Pet.hbm.xml:

<many-to-one name="user" class="HibernateBeans.User" fetch="select">
        <column name="UserID" not-null="true" />
</many-to-one>

Any help at all on this issue would be greatly appreciated. Although this is my first time posting a question, StackOverflow has saved my life on many an occasion.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:50:56+00:00Added an answer on May 20, 2026 at 5:50 pm

    Are you using both the XML and annotations based mapping in the same project? I would recommend using a single method (in your case annotations).

    Your XML version of mapping is not declaring the User.pets as eagerly loaded property. That could be the reason you are seeing an empty list.

    EDIT:

    As you are still getting the issue after switching over to only xml mappings, I suggest trying the following:

    1. Make sure you have got the User object from hibernate and not created with new keyword
    2. Try declaring the User.pets as eagerly loaded by using lazy="false" in your mapping
    3. Calling the user.getPets() immediately after loading the User (i-e without closing the session that loaded the user) (see #1)

    Points 2 and 3 are just for giving you a lead in debugging. Given that you are not eagerly loading pets, any attempt to call user.getPets() after closing the original session should result in an org.hibernate.LazyInitializationException.

    EDIT:

    One more thing to check would be that you are not manually assigning a value to User.pets field. Hibernate supplies a custom implementation of collections to be able to provide the lazy initialization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.