为什么我的“一对多” Hibernate属性get方法返回一个空集合?
我已经为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,为了在保存用户对象时保存宠物对象的集合,您必须将一对多映射中的级联属性设置为“全部”。下面指定。
试试这个吧,也许有用。
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.
Try this out.It may work.
您是否在同一个项目中同时使用基于 XML 和注释的映射?我建议使用单一方法(在您的情况下注释)。
您的 XML 版本的映射未将 User.pets 声明为急切加载的属性。这可能就是您看到空列表的原因。
编辑:
由于您在切换到仅 xml 映射后仍然遇到问题,我建议尝试以下操作:
new 关键字
lazy="false"
将 User.pets 声明为急切加载第 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:
new
keywordlazy="false"
in your mappingPoints 2 and 3 are just for giving you a lead in debugging. Given that you are not eagerly loading
pets
, any attempt to calluser.getPets()
after closing the original session should result in anorg.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.