Sakthi Draggerz
Sakthi Draggerz

Reputation: 101

How to use Contains in Hibernate

session = sessionFactory.openSession();         
Transaction tx = session.beginTransaction();
String hql ="from CustomObjectId where CONTAINS(fileName,fileName)";
Query query = session.createQuery(hql);
query.setParameter("fileName", fileName);
List file=query.list();
tx.commit();

How can I search CONTAINS?

Upvotes: 0

Views: 4672

Answers (2)

Vasu
Vasu

Reputation: 22422

You need to use IN clause as shown below:

 session = sessionFactory.openSession();         
String hql =" from CustomObjectId c where c.fileName in :fileName";
Query query = session.createQuery(hql);
query.setParameter("fileName", fileName);
List file=query.list();

Also, you DO NOT need a transaction (explicitly) to only READ from database. So, just remove Transaction tx = session.beginTransaction(); and tx.commit(); lines.

Upvotes: 1

Afsun Khammadli
Afsun Khammadli

Reputation: 2068

You can do that with Hibernate Criteria.

String fileNames[] = {"fileName1", "fileName2"};
session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(CustomObjectId.class);
criteria.add(Restrictions.in("fileName ", fileNames));
List list = criteria.list();

Upvotes: 0

Related Questions