1 / 11

Criteria

Criteria. Når og hvorfor bruke Criteria i Hibernate. Hva er Criteria. Et alternativ til HQL Representerer en spørring mot en enkelt, persistent klasse Innsnevring av søket ved å legge til restriksjoner (Restrictions). Hvorfor Criteria?. Gi renere Java-kode ved et avansert søk

manning
Télécharger la présentation

Criteria

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Criteria Når og hvorfor bruke Criteria i Hibernate

  2. Hva er Criteria • Et alternativ til HQL • Representerer en spørring mot en enkelt, persistent klasse • Innsnevring av søket ved å legge til restriksjoner (Restrictions)

  3. Hvorfor Criteria? • Gi renere Java-kode ved et avansert søk • Bedre støtte for unike resultater ved mange foreninger av andre tabeller

  4. SQL vs HQL vs. Criteria Alle rader SQL SELECT * FROM user; HQL Query query = getSession().createQuery(”from User”); Criteria Criteria criteria = getSession.createCriteria(User.class);

  5. SQL vs HQL vs. Criteria (frts.) Alle rader med e-post lik … SQL SELECT * FROM user WHERE email = ?; HQL Query query = getSession().createQuery(”from User u where u.email = :email”); query .setParameter(”email”, email); Criteria Criteria criteria = getSession.createCriteria(User.class); If (email != null && email.length() > 0) critera.add(Restriction.eq(”email”, email);

  6. Så hva er ”greia”? • What’s the big deal, lizm..? • For enklere spørringer gir det tyngre syntaks. Bruk HQL! • For kompliserte spørringer er det motsatt. Eksempel på HQL følger:

  7. Eksempel på tyngre HQL String hql = ”from Project p where lower(p.title) like lower (:searchString) or lower (p.user.unit) like lower (:searchString) or lower (p.user.fullname) like lower (:searchString)”; if (fromDate != null) hql += ” and p.dateRegistered > :fromDate”; If (toDate != null) hql += ” and p.dateRegistered < :toDate”; Query query = getSession.createQuery(hql); query.setParameter(”searchString”, ”%” + searchString + ”%”; if (fromDate != null) query.setParameter(”fromDate”, fromDate); if (toDate != null) query.setParameter(”toDate”, toDate);

  8. Oversatt til Criteria Criteria criteria = getSession().createCriteria(Project.class); criteria.addAlias(”user”, u); criteria.add(Restriction.disjunction() .add(Restriction.ilike(”title”, searchString, MatchMode.ANYWHERE)) .add(Restriction.ilike(”u.unit”, searchString, MatchMode.ANYWHERE)) .add(Restriction.ilike(”u.fullname”, searchString, MatchMode.ANYWHERE))); if (fromDate != null) criteria.add(Restriction.gt(”dateRegistered”, fromDate); If (toDate != null) criteria.add(Restriction.lt(”dateRegistered”, toDate);

  9. Når brukes Criteria? • Når en ellers må bygge opp HQL-strengen med mange parametere, ved ”hql += …” • Ved komplekse AND- og OR-søk blandet • Når en ønsket et unikt resultat fra forening med mange andre tabeller. HQL støtter ikke dette. Criteria: criteria.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY);

  10. Ikke overbevist? • Q: Criteria er mye tyngre enn HQL • A: Ja! • Q: Og derfor? • A: Vel, hvis du synes det er bedre å lese følgende kode, så og ahead! Og husk at du i tillegg må implementere fjerning av dupliserte rader fra resultatet ved forening av mange tabeller!

  11. For de HQL-frelste tring hql = "select project from Project project join project.user user where \n"                 + "( lower(project.title) like lower(:searchString) \n"                 + "  or lower(user.unit) like lower(:searchString) \n"                 + "  or lower(user.fullname) like lower(:searchString) ) \n";         if (doBothDateSearch) {             hql = hql + " and ( project.dateRegistered > :fromDate \n";             hql = hql + "       and project.dateRegistered < :toDate ) \n";         }         if (doFromDateSearch) {             hql = hql + " and project.dateRegistered > :fromDate \n";         }         if (doToDateSearch) {             hql = hql + " and project.dateRegistered < :toDate \n";         }         if (infoDelivered == true) {             hql = hql + " and lower(project.informationDelivered) like 'ja' \n";         }         if (students) {             hql = hql + " and 1 member of project.categories \n";         }         if (employees) {             hql = hql + " and 2 member of project.categories \n";         }         if (others) {             hql = hql + " and 3 member of project.categories \n";         }         Query query = getSession().createQuery(hql);         query.setParameter("searchString", "%" + searchString + "%");         if (doBothDateSearch) {             query.setParameter("fromDate", fromDate);             query.setParameter("toDate", toDate);         }         if (doFromDateSearch) {             query.setParameter("fromDate", fromDate);         }         if (doToDateSearch) {             query.setParameter("toDate", toDate);         }

More Related