Example usage for org.hibernate.query Query setEntity

List of usage examples for org.hibernate.query Query setEntity

Introduction

In this page you can find the example usage for org.hibernate.query Query setEntity.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
Query<R> setEntity(String name, Object val);

Source Link

Document

Bind an instance of a mapped persistent class to a named query parameter.

Usage

From source file:DAO.FixingDAO.java

@Override
public List<Fixing> findFixingsByImmatriculation(String immatriculation) {

    // new DAO with get previous session (false)
    CarDAO carDao = new CarDAO(false);

    // get car by immatriculation
    Car car = carDao.getCarByImmatriculation(immatriculation);

    System.out.println("Car found " + car.getImmatriculation());

    // reopen the session (because CarDAO has closed it)
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    transaction = session.beginTransaction();

    String hql = "FROM Fixing WHERE  car = :car ";
    Query query = session.createQuery(hql);
    query.setEntity("car", car);

    List<Fixing> fixings = query.getResultList();

    session.close();/*from w w w  .  j a v  a 2s.c o m*/

    return fixings;

}

From source file:DAO.FixingDAO.java

@Override
public List<Fixing> findFixingsByDepartement(String departementName) {

    session = HibernateUtil.getSessionFactory().getCurrentSession();
    transaction = session.getTransaction();

    // new DAO with get previous session (false)
    CarDAO carDao = new CarDAO(false);

    // get cars by departement
    List<Car> cars = carDao.getCarByDepartement(departementName);

    List<Fixing> fixings = new ArrayList<>();

    // reopen the session (because CarDAO has closed it)
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    transaction = session.beginTransaction();

    // for each car in that departement , we will get all its fixings
    for (Car car : cars) {
        String hql = "FROM Fixing WHERE  car = :car ";
        Query query = session.createQuery(hql);
        query.setEntity("car", car);

        try {// w  w  w .j  a  v a  2 s.c  o  m
            fixings.addAll(query.getResultList());
        } catch (NullPointerException e) {
            /// this car does not have any Fixings
        }
    }

    session.close();

    return fixings;

}