JPA @OneToMany结果为空

时间:2013-04-16 20:32:44

标签: java mysql jpa eclipselink

我正在尝试通过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;
    ... 
}

每当我尝试将部件连接到所有者时,生成的Set都为空。在打电话之后。 size()方法,它仍然是空的。

List<Owner> owners = em.createQuery("...", Owner.class).getResultList();

for (Owner o : owners) {
    System.out.print(o + ": ");
    Set<Part> parts = o.getParts();
    parts.size()
}

此处部件Set始终为空。在数据库中,它似乎已连接,但我无法从中获得任何结果。我做错了什么?

1 个答案:

答案 0 :(得分:0)