Ebean:一对多关系的where子句
Ebean: where clause on one to many relationship
我有两个模型 - 作者,书籍,如下所示。
我想根据年龄查找有关作者的详细信息,并获取该作者撰写的所有未删除书籍。
作者
@Entity
@Table(name = "author")
public class Author extends Model {
@Id
@Column(name = "id")
public int id;
@Column(name = "name")
public String name;
@Column(name = "age")
public int age;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
public List<Book> books;
private static Finder<Integer, Author> find = new Finder<Integer, Author>(Integer.class, Author.class);
public static List<Author> findByAge(int age) {
return find.fetch("books")
.where()
.eq("age", age)
.eq("books.isDeleted", false)
.findList();
}
图书实体:
@Entity
@Table(name = "book")
public class Book extends Model {
@Id
@Column(name = "id")
public int id;
@ManyToOne(targetEntity = Author.class)
@JoinColumn(name = "author_id")
public Author author;
@Column(name = "name")
public String name;
@Column(name = "is_deleted")
public boolean isDeleted;
}
作者实体中的查找器有问题吗?
我是否获取所有书籍,然后过滤 Java 中所有未删除的书籍?
提前致谢
我可以看到几个问题。您的模型应该扩展模型,并且您的书籍实体没有 isDeleted 字段(您可能想要一个 "public boolean deleted",并将代码更改为 "books.deleted",false)
除此之外,它看起来应该可行。
我有两个模型 - 作者,书籍,如下所示。 我想根据年龄查找有关作者的详细信息,并获取该作者撰写的所有未删除书籍。
作者
@Entity
@Table(name = "author")
public class Author extends Model {
@Id
@Column(name = "id")
public int id;
@Column(name = "name")
public String name;
@Column(name = "age")
public int age;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
public List<Book> books;
private static Finder<Integer, Author> find = new Finder<Integer, Author>(Integer.class, Author.class);
public static List<Author> findByAge(int age) {
return find.fetch("books")
.where()
.eq("age", age)
.eq("books.isDeleted", false)
.findList();
}
图书实体:
@Entity
@Table(name = "book")
public class Book extends Model {
@Id
@Column(name = "id")
public int id;
@ManyToOne(targetEntity = Author.class)
@JoinColumn(name = "author_id")
public Author author;
@Column(name = "name")
public String name;
@Column(name = "is_deleted")
public boolean isDeleted;
}
作者实体中的查找器有问题吗? 我是否获取所有书籍,然后过滤 Java 中所有未删除的书籍?
提前致谢
我可以看到几个问题。您的模型应该扩展模型,并且您的书籍实体没有 isDeleted 字段(您可能想要一个 "public boolean deleted",并将代码更改为 "books.deleted",false)
除此之外,它看起来应该可行。