Search code examples
hibernatejpahibernate-criteria

Hibernate criteria filter by foreign key


I have two tables: authors and books. Authors has two colums: int id, varchar authorName. Books has three colums: int id, varchar bookName, int authorId.

Now considering that I take the authorName as an input String filtering criteria how can I make hibernate only return the books by that certain author?


Solution

  • How about

    Criteria criteria = session.createCriteria( Book.class );
    criteria.createCriteria( "author", "a");
    criteria.add( Restrictions.eq( "a.authorName", "YOUR_INPUT"));
    criteria.list();