我正在尝试通过 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;
...
}
每当我尝试将部件连接到所有者时,结果集都是空的。打电话后,例如。size() 方法,它仍然是空的。
List<Owner> owners = em.createQuery("...", Owner.class).getResultList();
for (Owner o : owners) {
System.out.print(o + ": ");
Set<Part> parts = o.getParts();
parts.size()
}
这里的零件集总是空的。在数据库中它似乎已连接,但我无法从中获得任何结果。我究竟做错了什么?