문제

In play framework, I use following code to fetch values from a table called "Report" which has other relationship tables like "Project","Build" etc.

List<Report> rpts = Report.find.where()
                            .eq("publish","1")
                            .eq("functionality_id", Integer.toString(fun.id))
                            .eq("project_id", currentProject.id)
                            .eq("prod_build", prod_build)
                            .eq("loadType_id", loadType_id)
                            .in("build_id", buildId)                            
                            .orderBy("id desc")                                             
                            .findList();

I get list of values from "Report" table, but all related table values are not populated. They are populated with null.

@Entity
@Table(name="report")
public class Report {

    @Id
    public int id;

    @Constraints.Required
    @ManyToOne
    @JoinColumn(name="build_id")
    public Build build;

@Constraints.Required
    @ManyToOne
    @JoinColumn(name="project_id")
    public Project project;
.
.
}

It was loaded with those values when I tested couple of days ago, but not working today. When I do rpts.get(i).build.release , it gives nullPointerException because build is null here. This code has not been changed in recent days. Wondering why this is happening. Can someone suggest me whether there is any other setting file (like application.conf) that does this lazy loading. Please help.

도움이 되었습니까?

해결책

I've resolved it.

The problem is that I created an Ebean transaction using following code that caused the trouble.

Ebean.beginTransaction();
        try{
            group.role = "A";
            Ebean.update(group);

            Ebean.commitTransaction();
        } finally {
            Ebean.endTransaction();
        }

I never suspected that this could would have caused the problem as I put this code in another class file that is not related to this page. Then I changed to following code and everything worked as expected.

@Transactional
public void saveGroup(Group group){

group.role = "A";    
Ebean.save(group);
.
.

}

Following documentation in play framework site helped me to identify the culprit. :)

Enhancement of direct Ebean field access (enabling lazy loading) is only applied to Java classes, not to Scala. Thus, direct field access from Scala source files (including standard Play 2 templates) does not invoke lazy loading, often resulting in empty (unpopulated) entity fields. To ensure the fields get populated, either (a) manually create getter/setters and call them instead, or (b) ensure the entity is fully populated before accessing the fields.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top