5

我正在使用 Play Framework 2.1.0 和 Ebean,在查询下列场景时遇到问题:

我有 3 个类代表数据库中的表。

class project {
   ...
   @OneToMany
   public SubProject sub;   
}

class SubProject {
   ...
   @ManyToOne
   public Project project;

   @OneToMany 
   public MiniProject mini;    
}   

class MiniProject {
  ...
  @ManyToOne
  public SubProject sub;    
}

使用 Ebean,如何

  • 检索项目列表的所有子项目?

  • 检索一个项目的所有小项目?

  • 给定子项目列表,如何检索所有小项目?

4

3 回答 3

3

首先,您错误地定义了您的类。您的 @OneToMany 注释应该在元素列表上定义。我更正了这些映射,然后编写了检索所需查询的测试方法。这是代码:

项目类:

@Entity  
public class Project {

    @Id
    public Long id;

   @OneToMany(mappedBy="project")
   public List<SubProject> subprojects;
}

子项目类:

@Entity 
public class SubProject {

    @Id
    public Long id;

    @ManyToOne
    public Project project;

    @OneToMany(mappedBy="subproject") 
    public List<MiniProject> miniprojects; 
} 

小项目类:

@Entity 
public class MiniProject {

    @Id
    public Long id;

    @ManyToOne
    public SubProject subproject;
}

测试方法:

@Test
public void subTest () {
    FakeApplication app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
    Helpers.start(app);

    Project p1 = new Project();
    p1.id=1L;

    SubProject s1 = new SubProject();
    SubProject s2 = new SubProject();
    s1.id=1L;
    s2.id=2L;

    p1.subprojects.add(s1);
    p1.subprojects.add(s2);
    s1.project = p1;
    s2.project = p1;

    MiniProject m1 = new MiniProject();
    MiniProject m2 = new MiniProject();
    MiniProject m3 = new MiniProject();
    MiniProject m4 = new MiniProject();

    m1.id=1L;
    m2.id=2L;
    m3.id=3L;
    m4.id=4L;

    s1.miniprojects.add(m1);
    s1.miniprojects.add(m2);
    s2.miniprojects.add(m3);
    s2.miniprojects.add(m4);

    m1.subproject =s1;
    m2.subproject =s1;
    m3.subproject =s2;
    m4.subproject =s2;

    Ebean.save(p1);
    Ebean.save(s1);
    Ebean.save(s2); 
    Ebean.save(m1);
    Ebean.save(m2);
    Ebean.save(m3);
    Ebean.save(m4);

    // retrieve all the subprojects of a list of Projects
    List<Long> projectIds = new ArrayList<Long>();
    projectIds.add(1L);     
    List<SubProject> subList = Ebean.createQuery(SubProject.class).where(Expr.in("project.id", projectIds)).findList();

    // retrieve all the miniprojects of a Project 
    Long projectId = 1L;
    List<MiniProject> miniList = Ebean.createQuery(MiniProject.class).where(Expr.eq("subproject.project.id", projectId)).findList();

    // given a list of sub projects , how to retrieve all the miniprojects
    List<Long> subprojectIds = new ArrayList<Long>();
    subprojectIds.add(1L);      
    List<MiniProject> miniSubList = Ebean.createQuery(MiniProject.class).where(Expr.in("subproject.id", subprojectIds)).findList();


    for(SubProject sub: subList) {
        System.out.println("subproject: "+sub.id);
    }
    System.out.println("-----------");
    for(MiniProject mini: miniList) {
        System.out.println("miniproject: "+mini.id);
    }
    System.out.println("-----------");
    for(MiniProject mini: miniSubList) {
        System.out.println("miniproject: "+mini.id);
    }
}
于 2014-12-07T01:02:28.813 回答
0

假设实体是固定的,并且实际上您的 OneToMany 关联具有 aList<SubProject>或 a Set<SubProject>(对于 MiniProject 也是如此):

检索项目列表的所有子项目?

select s from Project p inner join p.subProjects s where p.id in :projectIds

检索一个项目的所有小项目?

select m from Project p 
inner join p.subProjects s 
inner join s.miniProjects m
where p.id = :projectId

给定子项目列表,如何检索所有小项目。

与第一个查询相同:

select m from SubProject s inner join s.miniProjects m where s.id in :subProjectIds
于 2013-04-04T14:11:14.230 回答
0

在你的类中定义助手

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

查询:

SubProject.find.where().in("project", listOfProject)
MiniProject.find.where().eq("sub.project",yourProject)
MiniProject.find.where().in("sub", listOfSubProject)
于 2013-04-09T12:24:15.100 回答