I have a Parent class and a Child class with the current setup
@Entity
public class Parent {
@OneToMany(mappedBy = "parent")
private Set<Child> children;
@Column
private int type;
}
@Entity
public class Child {
@ManyToOne
@JoinColumn(name = "parent_id")
private Parent parent;
}
For some functions, I want to when querying the parent based on its type, the child is automatically retrieved and filled into the set.
This is what I have tried so far
res = getSession().createCriteria(
Parent.class, "parent"
).createAlias(
"parent.children", "children"
).add(
Restrictions.eq("parent.type", type)
).setFetchMode(
"children", FetchMode.JOIN // FetchMode.SELECT
).list();
When I run the unit test, the getter is always equals to Null (I do have data and relationship in database)
Parent parent = new Parent();
parent.setType(ParentType.WEEK);
parentDao.save(parent);
Child child = new Child();
child.setParent(parent);
childDao.save(child);
List<Parent> res = parentDao.findByType(0, 10, ParentType.WEEK, true);
assertNotNull(res.get(0).getChildren());
assertEquals(1, res.get(0).getChildren().size());
How can I fix this ?