0
获取中的问题。 我有类A和B 表A,BHibernate将父/子对象作为一行而不是收集对象的父对象
CLASS A:
private Integer id;
private String name;
private Set<B> bs = new HashSet<B>(0);
public A() {
}
public A(String name, Set<B> bs) {
this.name = name;
this.bs = bs;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "a")
public Set<B> getBs() {
return this.bs;
}
public void setBs(Set<B> bs) {
this.bs = bs;
}
B类:
private Integer id;
private A a;
private String BName;
public B() {
}
public B(A a){
this.a = a;
}
public B(A a, String BName) {
this.a = a;
this.BName = BName;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id", nullable = false)
public A getA() {
return this.a;
}
public void setA(A a) {
this.a = a;
}
@Column(name = "b_name", length = 45)
public String getBName() {
return this.BName;
}
public void setBName(String BName) {
this.BName = BName;
}
IN DB:甲分贝具有2个记录
- A有id 1 - >有2条记录B(表)
- A具有ID 2 - >有3个记录B(表)的
当我的查询用HQL/JPQL象下面这样: 查询查询= entityManager.createQuery(“选择一个来自A的联接取一个。 BS“); List list = query.getResultList();
我在列表中得到了5条记录,而不是2条与关联的孩子。
- A1 - > B1
- A1 - > B2
- A2 - > B3
- A2 - > B4
- A2 - > B5
,而不是领: 1 A1-> B收藏, 2. A2-> B收藏
我没有需要size()方法来加载父对象的集合数据。