Reputation: 1279
I have two entities connected via a many-to-one relationship
@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 ...
}
Now I want to get all B's which have a "accepted" status of A:
Criteria criteria = session.createCriteria(B.class);
criteria.createAlias("a.status", "status");
criteria.add(Restrictions.eq("status", "accepted"));
return criteria.list();
This cause hibernate to throw a "org.hibernate.QueryException: not an association: status". What I'm doing wrong? I thought the tables are associated because of the many-to-one relationship.
Upvotes: 0
Views: 729
Reputation: 6576
On @ManyToOne you forgot to mention on which column it is joining on. provide a @JoinColumn
and also specify mappedBy = 'a' / inverse = true etc.,
Check this
Update:
Criteria criteria = session.createCriteria(B.class,"b");
criteria.createAlias("b.a", "a");
criteria.add(Restrictions.eq("a.status", "accepted"));
return criteria.list();
Upvotes: 1