I used Hibernate 4.1.10.Final as the JPA provider, Spring and Spring MVC.There are two entities.
Code:
@Entity
@Table(name = "a")
public class A{
@Id
private String id;
@OneToMany(mappedBy = "a", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<B> bs;
....
}@Entity
@Table(name = "b")
public class B{
@Id
private String id;
@ManyToOne
@JoinColumn(name = "fk_a_id")
private A a;
....
}
I need to get an A and it's bs, so i using find(A.class,id) of EntityManager.
Code:
A a1 =em.find(A.class, id);
a1.getBs().size();Result is: the size of bs is zero. It means that there is no associated B.
But i'm sure that there are many associated Bs in the database and the data have been load from database while checking console.While i used Query to get that :
Code:
Query query = em.createQuery("SELECT a FROM A AS a WHERE a.id = ?1",A.class);
query.setParameter(1, id);
A a= (A) query.getSingleResult();
a.getBs().size(); // = 22This time the size is 22.What's wrong?JPA
Code:
@Entity
@Table(name = "a")
public class A{
@Id
private String id;
@OneToMany(mappedBy = "a", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<B> bs;
....
}@Entity
@Table(name = "b")
public class B{
@Id
private String id;
@ManyToOne
@JoinColumn(name = "fk_a_id")
private A a;
....
}
I need to get an A and it's bs, so i using find(A.class,id) of EntityManager.
Code:
A a1 =em.find(A.class, id);
a1.getBs().size();Result is: the size of bs is zero. It means that there is no associated B.
But i'm sure that there are many associated Bs in the database and the data have been load from database while checking console.While i used Query to get that :
Code:
Query query = em.createQuery("SELECT a FROM A AS a WHERE a.id = ?1",A.class);
query.setParameter(1, id);
A a= (A) query.getSingleResult();
a.getBs().size(); // = 22This time the size is 22.What's wrong?JPA
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货