为什么我的“一对多” Hibernate属性get方法返回一个空集合?

发布于 2024-10-24 08:50:56 字数 1396 浏览 3 评论 0原文

我已经为 User 类和 Pet 类(针对以宠物店为主题的学术 Web 项目)设置了“一对多”/“多对一”双向映射关系。每个用户都拥有宠物收藏。

看起来这种关系运行良好,因为当我在 NetBeans 的 HQL 查询浏览器中执行查询以选择数据库中的第一个用户时,我看到“Pets”属性列出了 2 个 Java 对象 ID,大概是我引用的两只宠物通过外键([HibernateBeans.Pet@76838e, HibernateBeans.Pet@b71950])。

令人沮丧(而且不正确)的是,当我传入与我成功查询的完全相同的用户时,下面的代码返回一个空集合:

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;
}

编辑:根据 Tahir 的建议,我已从 Java 中删除了注释类,并且现在严格使用 POJO XML 映射(但同样的问题仍然存在)。我的下面的源代码已相应更新:

以下是相关的 XML Hibernate 映射:

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>

关于此问题的任何帮助将不胜感激。虽然这是我第一次发布问题,但 StackOverflow 在很多场合救了我的命。

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-10-31 08:50:57

另外,为了在保存用户对象时保存宠物对象的集合,您必须将一对多映射中的级联属性设置为“全部”。下面指定。

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

试试这个吧,也许有用。

Also in order to save the collection of pet objects as u save an User object u have to make the cascade attribute in one to many mapping to "all".It's specified below.

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

Try this out.It may work.

轮廓§ 2024-10-31 08:50:57

您是否在同一个项目中同时使用基于 XML 和注释的映射?我建议使用单一方法(在您的情况下注释)。

您的 XML 版本的映射未将 User.pets 声明为急切加载的属性。这可能就是您看到空列表的原因。

编辑:

由于您在切换到仅 xml 映射后仍然遇到问题,我建议尝试以下操作:

  1. 确保您已从 hibernate 获取 User 对象,而不是使用 new 关键字
  2. 尝试在映射中使用 lazy="false" 将 User.pets 声明为急切加载
  3. 加载 User 后立即调用 user.getPets() (即不关闭加载 User.pets 的会话)用户)(参见 #1)

第 2 点和第 3 点只是为了引导您进行调试。鉴于您没有急于加载 pets,关闭原始会话后任何尝试调用 user.getPets() 都应该导致 org.hibernate.LazyInitializationException< /代码>。

编辑:

还要检查的一件事是您没有手动为 User.pets 字段分配值。 Hibernate 提供了集合的自定义实现,以便能够提供延迟初始化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文