0

I am newbie to Hibernate criteria and I got problem with simple obtain class. Assume we have classes:

    Class A{
    int id;
    List<A> aList;
    }

and:

  Class B{
    A a;
    (...)

}

and :

Class C{
int id;
String name;
B b;
}

I want to get List< C > if 'name' like 'abc'. Here is my criteria code:

    Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
    Criteria crit = session.createCriteria(C.class);
    crit.add(Restrictions.like("nazwa", "%"+string+"%").ignoreCase());
    return crit.list();

I got exception:

org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer

In my SQL query generated by criteria I can see "left outer join" on my A and B classes that are contained in my C class. Probably that's why I cannot load

Cichy
  • 1,319
  • 3
  • 20
  • 36

1 Answers1

1

There will be left outer joins if the associations are @OneToOne or @ManyToOne, just because they are eager in the ToOne owning side by default.

If you want to use such a LIKE clause, use MatchMode.ANYWHERE and ilike instead:

add(Restrictions.ilike("nazwa", string, MatchMode.ANYWHERE))

Also, make sure the property you want to query in class C is named nazwa (it is typed as name in the code example), and it has a proper getter/setter.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • yes, there are @ManyToOne annotations. How to drop left outer join in this criteria? I must select only my C class... – Cichy Oct 27 '11 at 11:39
  • You can find the answer to that question in this site easily: [How to stop Hibernate from eagerly fetching many-to-one associated object](http://stackoverflow.com/q/222453/851811). Have you tried `@ManyToOne(fetch=FetchType.LAZY)` ? – Xavi López Oct 27 '11 at 11:41