Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package helpers; import java.util.List; import olym.*; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.hibernate.type.LongType; /** * * @author Amaterasu */ public class PanMelon { public static long countSportist(String sp, String zemlja) {//dohvata samo broj, a to i treba Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; long broj = 0; boolean good = false; tr = session.beginTransaction(); Query q = session.createSQLQuery( "select count(DISTINCT sp.name) as cnt from sportista sp, spordisc sd where sp.nacionalnost='" + zemlja + "' and sd.sport='" + sp + "' and sd.id=sp.sport_discipline") .addScalar("cnt", LongType.INSTANCE); broj = (long) q.uniqueResult(); tr.commit(); good = false; session.close(); return broj; } public static List<Sportista> getSportistsSDZ(Spordisc sd, String zemlja) {//sve dohvata hib Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Sportista> sportists = null; boolean good = false; try { tr = session.beginTransaction(); Criteria crit = session.createCriteria(Sportista.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); crit.add(Restrictions.eq("spordisc", sd)); crit.createCriteria("zemlja").add(Restrictions.eq("naziv", zemlja)); crit.addOrder(Order.asc("name")); sportists = crit.list(); tr.commit(); good = false; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return sportists; } }