2017-02-25 7 views
2

그래서이 긴 complexe 기본은 SQLQuery 있습니다개의 HQL이 네이티브 SQL 쿼리를 변환하는 방법

string hql = 
    @"SELECT * 
    FROM 
    (SELECT a.*, rownum r__ 
    FROM 
    (select f2.filmid, 
    f2.realisateurid, 
    f2.titre, 
    f2.annesortie, 
    f2.langue, 
    f2.duree, 
    f2.resume, 
    f2.poster, 
    f2.qtytotal, 
    f2.qtydisponible from film f2 
    where f2.filmid in (
    select distinct f.filmid 
     from film f, filmpays fp, pays p, filmgenre fg, genre g, informationpersonnel director, role r, informationpersonnel actor 
     where f.filmid = fp.filmid 
     and fp.paysid = p.paysid 
     and f.filmid = fg.filmid 
     and fg.genreid = g.genreid 
     and f.realisateurid = director.personelid 
     and f.filmid = r.filmid 
     and r.personelid = actor.personelid 
     and f.qtydisponible > 0 
     and upper(f.titre) LIKE :titre 
     and f.annesortie >= :anneeLow AND f.annesortie <= :anneeHigh 
     and upper(g.Nomgenre) LIKE :genre 
     and upper(f.Langue) LIKE :langue 
     and upper(p.Nom) LIKE :pays 
     and upper(director.nom) LIKE :realisateur 
     and upper(actor.nom) LIKE :acteur) 
     order by f2.annesortie DESC, f2.titre) a 
     WHERE rownum < ((:page * 8) +1)) 
      WHERE r__ >= (((:page - 1) *8) +1) "; 
/*Begin transaction */ 
      ITransaction tx = s.BeginTransaction(); 
      IQuery query = s.CreateQuery(hql); 
      query.SetString("titre", "%" + sp.Title.ToUpper() + "%"); 
      query.SetInt32("anneeLow", sp.YearLow); 
      query.SetInt32("anneeHigh", sp.YearHigh); 
      query.SetString("pays", "%" + sp.Country.ToUpper() + "%"); 
      query.SetString("langue", "%" + sp.Lang.ToUpper() + "%"); 
    query.SetString("genre", "%" + sp.Genre.ToUpper() + "%"); 
     query.SetString("realisateur", "%" + sp.Director.ToUpper() + "%"); 
     query.SetString("acteur", "%" + sp.Actor.ToUpper() + "%"); 
     query.SetInt32("page", page); 
     IList<Film> movies = query.List<Film>(); 

     tx.Commit(); 

     return movies; 

을 그리고 난 100 % HQL 방식으로

유사

뭔가를 쓰기 위해 노력하고있어 내 쿼리처럼 complexe 다른 선택에서 선택이있는 경우 HQL의 내가 HQL에서 간단한 작은 쿼리를 만드는 방법을 이해하고있는 문서,하지만 컨설팅 후

IList<Cat> moreCats = sess.CreateQuery(
    "from Cat as cat where " + 
    "cat.Name = 'Fritz' or cat.id = :id1 or cat.id = :id2" 
).SetInt64("id1", id1) 
.SetInt67("id2", id2) 
.List<Cat>(); 

, 어떻게 진행해야합니까?

답변

3

HQL이 곳에서 문 때문에 하위 쿼리가 문제가 발생하지해야하는 위치에있는 하위 쿼리를 지원 감사드립니다.

from 문에서 하위 쿼리가 필요하지 않으면 더 이상 쿼리를 변경할 필요가 없습니다.

쿼리의 페이징 부분을 제거하면 도움이 될 것입니다. 이 페이징은 HQL 쿼리 개체에서 .SetFirstResult(indexCalculatedFromYourPage).SetMaxResults(yourPageSize)을 호출하여 수행해야합니다.

물론 엔티티의 필수 열을 모두 매핑해야합니다. 대부분의 질의는 이미 HQL과 호환됩니다.
관련 엔티티도 매핑하면 관련된 테이블의 조인 조건을 명시 적으로 작성하지 않아도되므로 where 하위 쿼리를 단순화 할 수 있습니다.

참고 : 검색어는 '여러 기준'검색어와 비슷합니다. 모든 매개 변수를 고려하여 하나의 쿼리를 작성하는 대신 (제공된 매개 변수를 비어 있거나 지정하지 않아도 됨) 일반적으로 제공되지 않은 매개 변수를 무시하기 위해 동적으로 쿼리를 작성하는 것이 더 쉽습니다. 그리고이를 위해 또는 과 같은 쿼리 API를 사용하는 것이 좋습니다.

편집 : 이제 귀하의 질문에 (.Net)과 (Java)으로 태그를 지정하셨습니다. 내가 연결된 두 API는 NHibernate를위한 것이다. 자바를 사용하고 있다면, API가 있고, 내가 모르는 다른 API가있을 것입니다.

+0

나는 100 번 upvote 할 수 있으면 좋겠다. 시간 내 주셔서 감사합니다. – napi15