OneToMany not populated in view but populated if loaded by Controller
-
11-12-2019 - |
Question
I am starting to learn Play Framework 2, and have met a strange problem.
I have a OneToMany
relationship defined between a Project
and its many Task
s.
@Entity
public class Project extends Model {
@OneToMany(mappedBy="project", cascade=CascadeType.ALL)
public List<Task> tasks = new ArrayList<Task>();
}
@Entity
public class Task extends Model {
@ManyToOne
public Project project;
}
I have some code that finds and displays a Project
:
final Project foundProject = Project.find(id);
//Logger.error("Tasks size:"+foundProject.tasks.size());
return ok(views.html.tasks.render(foundProject, taskForm));
And a view:
@for(task <- project.tasks) {
<li>@task.title</li>
}
Note the commented-out Logger
line in the code that finds the Project
. If that line is commented out, then no Task
s are shown, if the line is NOT commented out, then Task
s are shown.
It's as if some kind of lazy initialisation code works inside the Controller but not inside the View. Is there a subtlety of Play that I have missed, or am I doing something wrong?
Solution
It was my fault - I'm new to eBean and was doing this in the code to find my project:
finder.ref(id);
instead of:
finder.byId(id);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow