Search code examples
hibernatecriteriahibernate-criteria

Hibernate check equal of two unrelated column


I have sample entity as following

class Book{
  int id
  String name
  int sid
}

class Author {
   int id
   String name
   List<Book> books
   int mid
}

Its not good design, I know that just trying to explain a scenario.

Now my questions is, Is there any way I can check equal of two unrelated column

e.g

session.createCriteria(Author.class, "author")
       .createAlias("books", "book")
       .add(Restrictions.eq("author.mid","book.sid")
       .list()

Can it be done?


Solution

  • This is probably not the recommended way but Restriction.sqlRestriction worked for me to get the desired output

    session.createCriteria(Author.class, "author")
           .createAlias("books", "book")
           .add(Restrictions.sqlRestriction("author.mid","book.sid")
           .list()