我正在使用 HQL 进行动态查询,但由于参数很多,我相信 Criteria 会更加“可读”。查询如下所示:
String queryString = "select u from ReportSalesUnit as r join r.recipients as u where r.id = :reportSalesUnitId";
if ( firstName != null )
{
queryString += " and lower(u.firstName) like :firstName";
}
if ( lastName != null )
{
queryString += " and lower(u.lastName) like :lastName";
}
if ( email != null )
{
queryString += " and lower(u.email) like :email";
}
if ( phoneNr != null )
{
queryString += " and u.phoneNr like :phoneNr";
}
Query query = session.createQuery( queryString );
if ( firstName != null )
{
query.setParameter( "firstName", firstName.toLowerCase() + "%" );
}
if ( lastName != null )
{
query.setParameter( "lastName", lastName.toLowerCase() + "%" );
}
if ( email != null )
{
query.setParameter( "email", email.toLowerCase() + "%" );
}
if ( phoneNr != null )
{
query.setParameter( "phoneNr", phoneNr + "%" );
}
query.setParameter( "reportSalesUnitId", reportSalesUnitId );
老实说,我不知道如何返回与标准相关的联接。我在这里寻找答案,但找不到答案: http: //docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html,有人可以帮我吗?一点解释会很棒。