Example usage for org.hibernate.criterion Restrictions idEq

List of usage examples for org.hibernate.criterion Restrictions idEq

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions idEq.

Prototype

public static Criterion idEq(Object value) 

Source Link

Document

Apply an "equal" constraint to the identifier property

Usage

From source file:AddBooks.java

public static void main(String[] args) {
    try {//ww w. j a va2  s. co  m
        BooksHome bookDAO = new BooksHome();

        CategoriesHome categoryDAO = new CategoriesHome();
        Session categorySession = categoryDAO.getSessionFactory().getCurrentSession();
        categorySession.beginTransaction().begin();
        Criteria criteria = categorySession.createCriteria(Categories.class, "c")
                .add(Restrictions.idEq("MC_ENG"));

        List result = criteria.list();

        for (Object c : result) {
            cat = (Categories) c;
            System.out.println(cat.getCategoryName());
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        //Date date = dateFormat.parse("31/05/2011");
        Books book1 = new Books("ISBN1", cat, "introduction to java", "Dietil", dateFormat.parse("31/05/2011"),
                "Desktop", dateFormat.parse("21/03/2014"), "image1", 150, "Description1", 0);
        Books book2 = new Books("ISBN2", cat, "servlet", "Dietil", dateFormat.parse("5/05/2011"), "Enterprise",
                dateFormat.parse("21/03/2014"), "image2", 200, "Description2", 0);
        Books book3 = new Books("ISBN3", cat, "jsp", "Dietil", dateFormat.parse("28/07/2011"), "Enterprise",
                dateFormat.parse("21/03/2014"), "image3", 200, "Description3", 0);

        bookDAO.persist(book1);
        bookDAO.persist(book2);
        bookDAO.persist(book3);

        bookDAO.closeSession();

        System.out.println("Doneeeeeeeeeeeeeeeeeeeeee");

    } catch (ParseException ex) {
        Logger.getLogger(AddBooks.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:aa.PersonDog.dao.PersonDAO.java

public Person findPersonWithAllDogs(int personId) {

    Session session = HibernateFactory.openSession();

    Person p = (Person) session.createCriteria(Person.class).add(Restrictions.idEq(personId)).uniqueResult();
    // this will force SQL to execute the query that will join with the user's profile and populate  
    //  the appropriate information into the user object.  
    Hibernate.initialize(p.getDogs());/*from w  ww . j a v a2  s. c  o m*/

    return p;
}

From source file:alberto.spring.DAOMensaje.java

public Mensaje buscarPorId(Integer id) throws Exception {

    Mensaje m = (Mensaje) session.createCriteria(Mensaje.class).add(Restrictions.idEq(id)).uniqueResult();
    cerrarTodo();//w  w  w .  jav a  2 s . c o  m

    ///  c=(Cliente) session.createCriteria(Cliente.class).add(Restrictions.idEq(c.getIdCliente())).uniqueResult();

    return m;
}

From source file:at.ac.tuwien.ifs.tita.dao.GenericHibernateDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public T findById(ID id, String... joinProps) {
    T myEntity = null;/*from w w  w .  j a v  a 2s .co m*/
    Criteria criteria = null;

    try {
        criteria = getSession().createCriteria(this.persistenceClass);
        criteria.add(Restrictions.idEq(id));
        for (String prop : joinProps) {
            criteria.setFetchMode(prop, FetchMode.JOIN);
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        myEntity = (T) criteria.uniqueResult();
    } catch (Exception e) {
        throw new PersistenceException("Fehler beim lesen eines Entities: Class="
                + this.persistenceClass.getSimpleName() + " Key=" + id.toString() + "\n" + e.getMessage(), e);
    }

    return myEntity;

}

From source file:at.fhv.lindale.impl.hf.HibernateFacade.java

@Override
public I_File loadByID_File(int ID) {
    Session session = null;/*from w  w w.  j av  a  2  s  .  co m*/
    I_File ret = null;
    try {
        session = sessionFactory.openSession();

        ret = (I_File) session.createCriteria(StandardFile.class).add(Restrictions.idEq(ID)).uniqueResult();
    } catch (HibernateException ex) {

    } finally {
        if (session != null) {
            session.close();
        }
    }
    return ret;
}

From source file:at.fhv.lindale.impl.hf.HibernateFacade.java

@Override
public I_PluginInfo loadByID_PluginInfo(int ID) {
    Session session = null;//from   w  ww  . j  a  va 2 s .c  o  m
    I_PluginInfo ret = null;
    try {
        session = sessionFactory.openSession();

        ret = (I_PluginInfo) session.createCriteria(PluginInfo.class).add(Restrictions.idEq(ID)).uniqueResult();
    } catch (HibernateException ex) {

    } finally {
        if (session != null) {
            session.close();
        }
    }
    return ret;
}

From source file:at.fhv.lindale.impl.hf.HibernateFacade.java

@Override
public I_Collection loadByID_Collection(int ID) {
    Session session = null;/*from   w  w  w.  jav a 2 s .  c o m*/
    I_Collection ret = null;
    try {
        session = sessionFactory.openSession();

        ret = (I_Collection) session.createCriteria(Collection.class).add(Restrictions.idEq(ID)).uniqueResult();
    } catch (HibernateException ex) {

    } finally {
        if (session != null) {
            session.close();
        }
    }
    return ret;
}

From source file:at.fhv.lindale.impl.hf.HibernateFacade.java

@Override
public I_Source loadByID_Source(int ID) {
    Session session = null;/*www. j  av a2 s  .  c o  m*/
    I_Source ret = null;
    try {
        session = sessionFactory.openSession();

        ret = (I_Source) session.createCriteria(Source.class).add(Restrictions.idEq(ID)).uniqueResult();
    } catch (HibernateException ex) {

    } finally {
        if (session != null) {
            session.close();
        }
    }
    return ret;
}

From source file:au.org.theark.core.dao.StudyDao.java

License:Open Source License

public List<Study> getParentAndChildStudies(Long id) {
    Criteria studyCriteria = getSession().createCriteria(Study.class);
    Study study = getStudy(id);/*ww  w  .  jav  a  2  s . c  o m*/

    if (study.getStudyStatus() != null) {
        studyCriteria.add(Restrictions.eq(Constants.STUDY_STATUS, study.getStudyStatus()));
        try {
            StudyStatus status = getStudyStatus("Archive");
            studyCriteria.add(Restrictions.ne(Constants.STUDY_STATUS, status));
        } catch (StatusNotAvailableException notAvailable) {
            log.error("Cannot look up and filter on archive status. Reference data could be missing");
        }
    } else {
        try {
            StudyStatus status = getStudyStatus("Archive");
            studyCriteria.add(Restrictions.ne(Constants.STUDY_STATUS, status));
        } catch (StatusNotAvailableException notAvailable) {
            log.error("Cannot look up and filter on archive status. Reference data could be missing");
        }

    }

    if (study.getParentStudy() != null && !study.getParentStudy().equals(study)) {
        studyCriteria.add(
                Restrictions.or(Restrictions.idEq(id), Restrictions.eq("parentStudy", study.getParentStudy())));
    } else {
        studyCriteria.add(Restrictions.or(Restrictions.idEq(id), Restrictions.eq("parentStudy", study)));
    }

    studyCriteria.addOrder(Order.asc("id"));
    studyCriteria.addOrder(Order.asc(Constants.STUDY_NAME));
    return studyCriteria.list();
}

From source file:br.com.loja.framework.hibernate.dao.AbstractDAOHibernate.java

@Override
public T findByPrimaryKey(Object pk) {

    Class<?> type = ReflectionUtil.getFieldByAnnotation(clazz, Id.class).get(0).getType();

    pk = type.cast(pk);//from  w  w w  .jav  a2  s. c o  m

    if (pk == null) {
        throw new IllegalArgumentException("primary key type mismatch");
    }

    T entity = null;
    if (getSession() != null) {
        try {
            begin();
            entity = (T) getSession().createCriteria(clazz).add(Restrictions.idEq(pk)).setMaxResults(1)
                    .uniqueResult();
            commit();
        } catch (HibernateException e) {
            //TODO implemetar log 
            System.err.println(e.getMessage());
            rollback();
            e.printStackTrace();
        } finally {
            close();
        }
    }

    return entity;

}