0

I have two models - Author, Book as shown below. I want to find details about an author based on age and also fetch all non-deleted books written by that author.

Author

@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(); 

}

Book Entity:

@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;
}

Is there something wrong with my finder in Author entity. Do I fetch all books and then filter all the non-deleted ones in Java?

Thanks in advance

mancoolgunda
  • 87
  • 1
  • 3
  • 9

1 Answers1

0

I can see a couple of problems. Your models should extend Model, and your book entity doesn't have an isDeleted field (you probably want a "public boolean deleted", and change the code to "books.deleted", false)

Other than that, it looks like it should work.

Shanness
  • 365
  • 2
  • 10
  • thanks @Shanness, I have made the above mentioned changes. Now it returns me all the books including the deleted ones. I just want the non-deleted books. – mancoolgunda Apr 07 '15 at 14:45
  • Hmm, not sure, looks alright to me.. I'd suggest logging the database calls to see what's happening, and makes the DB rows are correct. `db.default.logStatements=true logger.com.jolbox=DEBUG` See this http://stackoverflow.com/questions/9719601/play-framework-2-0-and-ebean-sql-logging – Shanness Apr 13 '15 at 13:27