Reputation: 511
I am working with two entities - Top
and Package
. Top
looks like the below.
@Entity
@Table(name = "top_top")
public class Top implements Serializable {
private int id;
private Package pkg;
private int package_ref;
@Id
@Column(name="id", unique=true)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "package_ref", insertable=false, updatable=false)
public Package getPackage() {
return pkg;
}
public void setPackage(Package pkg) {
this.pkg = pkg;
}
}
And Package
looks like the below:
@Entity
@Table(name = "package_package")
public class Package implements Serializable {
private int id;
private Integer parent_id;
private Package parent;
@Id
@Column(name="id", unique=true)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id")
public Package getParent() {
return parent;
}
public void setParent(Package parent) {
this.parent = parent;
}
}
Given a Top
a, I want list of all Top
s whose Package
s have their parent as a.package.
To that end, I have tried
private List<Top> getSubTops(Top pkg_top) {
return getSession().createCriteria(Top.class).add(Restrictions.eq("package.parent.id", pkg_topo.getPackage().getId())).list();// Throws an error
// "tried package.parent_id". That throws an error too.
// In Django - I would have done Package.objects.filter(package__parent=a.package)
}
Upvotes: 0
Views: 14
Reputation: 4154
Try this:
private List<Top> getSubTops(Top pkg_top) {
return getSession().createCriteria(Top.class)
.createCriteria("package")
.add(Restrictions.eq("parent", pkg_top.getPackage()).list();
}
Upvotes: 1