我有两个通过多对一关系连接的实体
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "A_ID")
public Long id;
public String status;
@OneToMany(mappedBy = "a")
public Collection<B> b;
}
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="A_ID", nullable=false)
public A a;
// ... some other members ...
}
现在我希望得到所有具有“接受”状态A的B:
Criteria criteria = session.createCriteria(B.class);
criteria.createAlias("a.status", "status");
criteria.add(Restrictions.eq("status", "accepted"));
return criteria.list();
这导致hibernate抛出“org.hibernate.QueryException:not an association:status”。我做错了什么?我认为这些表是关系因为多对一的关系。
答案 0 :(得分:1)
在@ManyToOne上,您忘了提及它正在加入的列。提供@JoinColumn
并指定mappedBy ='a'/ inverse = true等,
检查this
<强>更新强>
Criteria criteria = session.createCriteria(B.class,"b");
criteria.createAlias("b.a", "a");
criteria.add(Restrictions.eq("a.status", "accepted"));
return criteria.list();