Example usage for org.hibernate SQLQuery addEntity

List of usage examples for org.hibernate SQLQuery addEntity

Introduction

In this page you can find the example usage for org.hibernate SQLQuery addEntity.

Prototype

SQLQuery<T> addEntity(Class entityType);

Source Link

Document

Declare a "root" entity, without specifying an alias.

Usage

From source file:MeetingApp.ParticipantMeetingHelper.java

public int getNumberMeeting() {

    List<ParticipantMeeting> meetingList = null;

    // create the query, but as a String
    String sql = "select * from Participant_Meeting";

    try {/*from ww  w.  jav a  2  s  . c o m*/
        // if the transaction isn't active, begin it
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // create the actual query that will get executed
        SQLQuery q = session.createSQLQuery(sql);

        // associate the Category POJO and table with the query 
        q.addEntity(ParticipantMeeting.class);

        // execute the query and cast the returned List
        // as a List of Films
        meetingList = (List<ParticipantMeeting>) q.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return meetingList.size();
}

From source file:MeetingApp.ParticipantMeetingHelper.java

public int getNumberparticipant(int meetingId) {

    List<Meeting> meetingList = null;

    // create the query, but as a String
    String sql = "select * from participant_meeting" + "where MEETING_ID = :meetingID";

    try {//from   ww w .  j av a 2 s. c  o  m
        // if the transaction isn't active, begin it
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // create the actual query that will get executed
        SQLQuery q = session.createSQLQuery(sql);

        // associate the Category POJO and table with the query 
        q.addEntity(Meeting.class);

        // execute the query and cast the returned List
        // as a List of Films
        q.setParameter("meetingID", meetingId);
        meetingList = (List<Meeting>) q.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return meetingList.size();
}

From source file:MeetingApp.ParticipantMeetingHelper.java

public int updateStatus(ParticipantMeeting a) {
    int result = 0;

    String sql = "update participant_meeting" + "set STATUS_ID = :statusId"
            + "where PARTICIPANT_EMAIL = :parEmail" + "and MEETING_ID = :meetingId";

    try {/* ww  w  .  j a  va  2s  .  c om*/
        // starting a transaction if on wisn't active
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        // creating an actul query that can be executed
        SQLQuery q = session.createSQLQuery(sql);
        //associating our Avtor POJO and table with the query 
        q.addEntity(ParticipantMeeting.class);

        int num = a.getMeeting().getMeetingId();
        String em = a.getParticipant().getParticipantEmail();
        // binding values to the placeholders in the query
        q.setParameter("meeting", a.getMeeting().getMeetingId());
        q.setParameter("participant", a.getParticipant().getParticipantEmail());
        q.setParameter("status", a.getStatus().getStatusId());

        // executing the query 
        result = q.executeUpdate();

        // commiting the query to the database
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Metodos.Dao.java

public static List<Grupo> getGrupos(int turno) {

        String consulta = "select * from Grupo where IdTurno";
        Session session = NewHibernateUtil.sessionFactory.openSession();
        SQLQuery query = session.createSQLQuery(String.format(consulta));
        List<Grupo> grupos = query.addEntity(Grupo.class).list();
        session.close();/*from w  w  w. java2 s.co  m*/

        return grupos;

    }

From source file:model.AppointmentModel.java

public List<Appointment> findAppointment(Date date) {
    List<Appointment> app = new ArrayList<>();

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*from ww  w. ja  v a  2  s . c om*/
    String sql = "SELECT * FROM Appointment WHERE dateofAddAppointment = '" + date.getYear() + "-"
            + date.getMonth() + "-" + date.getDate() + "'";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Appointment.class);
    List results = query.list();
    for (Object l : results) {
        app.add((Appointment) l);
    }
    session.getTransaction().commit();

    return app;
}

From source file:model.AppointmentModel.java

public Appointment findAppointmentPesel(Date date, String pesel) {
    Appointment app = null;/*from  ww w. j ava 2  s. co m*/
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    String sql = "SELECT * FROM Appointment WHERE dateofAddAppointment = '" + (1900 + date.getYear()) + "-"
            + (1 + date.getMonth()) + "-" + date.getDate() + "'" + " and pesel = '" + pesel + "'";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Appointment.class);
    List results = query.list();
    for (Object l : results) {
        app = (Appointment) l;
    }
    session.getTransaction().commit();
    return app;
}

From source file:model.CustomerModel.java

public Customer findCustomer(Customer customer) {
    Customer cust = null;/*from  w  ww  .jav  a  2  s.c o  m*/
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    String sql = "SELECT * FROM Customer WHERE firstName = :firstName" + " and lastName = :lastName"
            + " and adress = :adress" + " and pesel = :pesel" + " and phone = :phone" + " and email = :email";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Customer.class);
    query.setParameter("firstName", customer.getFirstName());
    query.setParameter("lastName", customer.getLastName());
    query.setParameter("adress", customer.getAdress());
    query.setParameter("pesel", customer.getPesel());
    query.setParameter("phone", customer.getPhone());
    query.setParameter("email", customer.getEmail());
    List results = query.list();
    for (Object l : results) {
        cust = (Customer) l;
    }
    session.getTransaction().commit();

    return cust;
}

From source file:ModelDao.GenericDAOImpl.java

public void delete(Object object, String table, String id) {
    Session session = sessionFactory.openSession();
    Transaction tx = null;/*from ww  w . j a v  a 2 s.c o  m*/
    try {
        tx = session.beginTransaction();
        String sql = "UPDATE " + table + " set d_e_l_e_t_e = '1' WHERE ID" + table + "= '" + id + "'";
        System.out.println(sql);
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(object.getClass());
        query.executeUpdate();
        tx.commit();

    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
}

From source file:ModelDao.GenericDAOImpl.java

public List getDados(Class object, String table, String id) {
    Session session = sessionFactory.openSession();
    Transaction tx = null;//from w ww  . ja  va2  s .c o m
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM " + table + " WHERE ID" + table + " = '" + id + "'";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(object);
        List result = query.list();
        tx.commit();

        return result;
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return null;
}

From source file:ModelDao.GenericDAOImpl.java

public void update(Class clazz, String table, String id, String stringUpdate) {
    Session session = sessionFactory.openSession();
    Transaction tx = null;/*  ww w .  ja va 2 s.c  om*/
    try {
        session.beginTransaction();
        String sql = "UPDATE " + table + " set " + stringUpdate + " WHERE ID" + table + " = '" + id + "'";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(clazz);
        int result = query.executeUpdate();
        System.out.println("Rows affected: " + result);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
}