Reputation: 433
I have a question concerning Ebean. Unfortunately I cannot find any documentation to help me. I would like to select all rows where a value is true and where all members of a OneToMany relationship also have a value which is true.
Here is an example:
@Entity
public class Book extends Model {
@Id
public Long id;
public Boolean isAvailable;
public static Finder<Long, Book> find = new Finder<>(Long.class, Book.class);
}
I would like to complete the findAllByIsAvailable method:
@Entity
public class Category extends Model {
@Id
public Long id;
public Boolean isAvailable;
@OneToMany(mappedBy="category", cascade=CascadeType.ALL)
public List<Book> books;
public static Finder<Long, Category> find = new Finder<>(Long.class, Category.class);
public static List<Category> findAllByIsAvailable() {
// Find all categories where isAvailable == true and books.isAvailable == true
}
}
Upvotes: 0
Views: 922
Reputation: 151
First you need to add a @ManyToOne
relationship to your Book
class as :
@Entity
public class Book extends Model {
@Id
public Long id;
public Boolean isAvailable;
@ManyToOne
public Category category;
public static Finder<Long, Book> find = new Finder<>(Long.class, Book.class);
}
Then you will be able to insert in your findAllByIsAvailable()
function this
return Category.find.fetch("books").where().eq("isAvailable",true).eq("books.isAvailable",true).findList();
.fetch("books")
means that you make a left join of two tables.
You can read a lot about Ebean here http://www.avaje.org/doc/ebean-userguide.pdf There also are some examples.
Upvotes: 1