休眠限制,在with and中,如何使用?

mbskvtky  于 2022-10-15  发布在  Java
关注(0)|答案(3)|浏览(125)

我有下面这样的table

id, employee_no, survey_no, name
1    test          1         test_name
2    test2         1         test_name2
3    test3         1         test_name3
4    test4         2         test_name4

如何通过将下面的AND组合成一个in语句来查询Restriction.in?

IN[   (if(survey_no==1)  && employee_no== 'test')  ,  
       (if(survey_no==1)  && employee_no== 'test2') ,
        ...
   ]
uwopmtnx

uwopmtnx1#

我认为这是您想要使用的条件组合(顺便说一句,帮助定义Hibernate实体bean比帮助定义表结构更容易):

String[] employeeNames = { "test", "test2" };
List<Survey> surveys = getSession().createCriteria(Survey.class).add(
        Restrictions.and
        (
            Restrictions.eq("surveyNumber", 1),
            Restrictions.in("employeeName", employeeNames)
        )
    ).list();
fwzugrvs

fwzugrvs2#

List<EmployeeSurvey> employeeSurvey = getSession().createCriteria
(EmployeeSurvey.class).add(
    Restrictions.and
    (
        Restrictions.eq("survey_no", 1), 
        Restrictions.in("employee_name", new String[] {"test", "test1"})
    )).list();
nbysray5

nbysray53#

public List<Breed> getManyBreeds(StringBuilder breedIds)  {
        System.out.println(breedIds);
        SessionFactory factory = HibernateConnection.getConnection();
        Session session = factory.openSession();
        Criteria criteria = session.createCriteria(Breed.class);
        criteria.add(Restrictions.in("id", breedIds.toString()));
        List<Breed> breeds = criteria.list();
        session.close();
        return breeds;
    }

我遇到了同样的问题,我已将所有ID存储在stringBuilder中,但当我尝试此方法时,它会给我一个错误,没有返回任何值,请帮助我解决此问题。

相关问题