如何在Hibernate中使用Contains

时间:2016-11-07 12:43:12

标签: java spring 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();

如何搜索CONTAINS

2 个答案:

答案 0 :(得分:1)

您需要使用IN子句,如下所示:

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

此外,您不需要事务(显式)只从数据库读取。所以,只需删除Transaction tx = session.beginTransaction(); and tx.commit();行。

答案 1 :(得分:0)

您可以使用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();