[英]JPA @OneToMany results empty
I'm trying acces a MySQL databese through JPA, but the result is always empty. 我正在尝试通过JPA访问MySQL数据库,但结果始终为空。
public class Part {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner", referencedColumnName = "id")
private Owner owner;
...
}
public class Owner {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
...
@OneToMany(mappedBy = "owner")
private Set<Part> parts;
...
}
Whenever I try to get the parts connected to an owner, the resulting Set is empty. 每当我尝试将零件连接到所有者时,结果集都是空的。 After calling eg.
在致电例如。 size() method, it's still empty.
size()方法,它仍然为空。
List<Owner> owners = em.createQuery("...", Owner.class).getResultList();
for (Owner o : owners) {
System.out.print(o + ": ");
Set<Part> parts = o.getParts();
parts.size()
}
Here the parts Set is always empty. 在此零件集始终为空。 In the database it seems to be connected, but I can't get any results out of it.
在数据库中它似乎已连接,但我无法从中获得任何结果。 What am I doing wrong?
我究竟做错了什么?
My guess is that you are not adding to both sides of your relationship when creating your objects. 我的猜测是,在创建对象时,您不会在关系的两面都加添麻烦。
See, http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side 参见http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_update__after_updating_the_other_side
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.