4

Hi I could need a little help understanding how I should approach following:

Just for information I'm working with play framework 2.0 and Ebeans version ~2.7

I have a model projects and a model reports. A project can have many reports but a report can only belong to one project.

This is my Projects Model:

@Entity
@Table(name = "Projects")
public class Projects extends Model {
    private static final long serialVersionUID = -3343133304437498550L;

   @Id
   public long id;

   @NotNull
   @Size(min = 1, max = 255)
   public String name;

   @NotNull
   @Size(min = 1, max = 255)
   public String description;

   @Formats.DateTime(pattern = "dd/MM/yyyy")
   public Date created_at = new Date();

   public static Finder<Long, Projects> find = new Finder<Long, Projects>(Long.class, Projects.class);

}

And this is my Reports Model:

@Entity
@Table(name = "Reports")
public class Reports extends Model {
    private static final long serialVersionUID = -6617128169471038678L;

    @Id
    public long id;

    @Constraints.Required
    public String description;

    @Formats.DateTime(pattern = "dd/MM/yyyy")
    public Date created_at = new Date();

    @ManyToOne
    @NotNull
    public Projects project;

    public static Finder<Long, Reports> find = new Finder<Long, Reports>(Long.class, Reports.class);
}

The saving works without a problem I can create a Project and I can create multiple reports that point to the right projects. My question know is how would I go about querying this.

When I have a project how would I get all related reports to it? I think I could figure it out as a plain SQL-Query but I'm pretty sure it should be possible with ebean.

Best regards and thank you for your time!

UPDATE: This is how I would do it with a sql query

SELECT * FROM `Projects` LEFT JOIN `Reports` ON Projects.`id` = Reports.`id`;
4

1 に答える 1

4

今は試すことができませんが、次のようにします。

public class Reports extends Model {

...

  public static List<Reports> findByProject(Projects project) {
    return find.fetch("project").where().eq("project.id", project.id).findList();
  }
}
于 2013-02-24T14:07:38.350 に答える