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:it.mdps.gestguide.database.dao.hibernate.IstruttoreDaoImpl.java

@SuppressWarnings("unchecked")
@Override//  w ww .ja va  2 s . com
@Transactional(readOnly = true)
public List<Istruttore> getAvailableInstructors(int schoolId, int licenseId, Date fromDate, Date toDate) {
    Session session = super.sessionFactory.getCurrentSession();
    String sql = "select * FROM Istruttore WHERE id_autoscuola = :schoolId " + " AND id_istruttore in ( "
            + "      SELECT a.id_istruttore FROM Abilitazione a, Istruttore i " // seleziona istruttori abilitati alla patente richiesta e appartenenti alla scuola
            + "      WHERE i.id_autoscuola = :schoolId AND i.id_istruttore=a.id_istruttore "
            + "      AND id_patente = :licenseId)" + "   AND id_istruttore NOT IN ("
            + "      SELECT id_istruttore FROM Prenotazione " // seleziona istruttori impegnati in quell'arco temporale
            + "      WHERE id_autoscuola = :schoolId " + "      AND ("
            + "         (:fromDate BETWEEN data_inizio AND data_fine) " + "         OR"
            + "         (:toDate BETWEEN data_inizio AND data_fine)" + "         OR"
            + "         (:fromDate <= data_inizio AND :toDate >= data_fine)" + "      )" + ")";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Istruttore.class);

    query.setInteger("schoolId", schoolId);
    query.setInteger("licenseId", licenseId);
    query.setDate("fromDate", fromDate);
    query.setDate("toDate", toDate);

    return query.list();
}

From source file:it.mdps.gestguide.database.dao.hibernate.MezzoDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*from  www .ja v  a  2s  .  c  o m*/
@Transactional(readOnly = true)
public List<Mezzo> getAvailableVehicles(int schoolId, int licenseId, Date fromDate, Date toDate) {
    Session session = super.sessionFactory.getCurrentSession();

    String sql = "select * FROM Mezzo WHERE id_autoscuola = :schoolId " + " AND id_mezzo in ( "
            + "      SELECT a.id_mezzo FROM Abilitazione_Mezzo a, Mezzo m " // seleziona mezzi abilitati alla patente richiesta e appartenenti alla scuola
            + "      WHERE m.id_autoscuola = :schoolId AND m.id_mezzo=a.id_mezzo "
            + "      AND id_patente = :licenseId)" + "   AND id_mezzo NOT IN ("
            + "      SELECT id_mezzo FROM Prenotazione " // seleziona mezzi impegnati in quell'arco temporale
            + "      WHERE id_autoscuola = :schoolId " + "      AND ("
            + "         (:fromDate BETWEEN data_inizio AND data_fine) " + "         OR"
            + "         (:toDate BETWEEN data_inizio AND data_fine)" + "         OR"
            + "         (:fromDate <= data_inizio AND :toDate >= data_fine)" + "      )" + ")";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Mezzo.class);

    query.setInteger("schoolId", schoolId);
    query.setInteger("licenseId", licenseId);
    query.setDate("fromDate", fromDate);
    query.setDate("toDate", toDate);

    return query.list();
}

From source file:MeetingApp.HostHelper.java

public int insertHost(Host a) {
    int result = 0;

    String sql = "insert into Host( HOST_EMAIL, HOST_NAME) " + "values (:email, :name)";

    try {/* w  w w .  ja  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(Host.class);

        // binding values to the placeholders in the query
        q.setParameter("name", a.getHostName());
        q.setParameter("email", a.getHostEmail());

        // 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:MeetingApp.HostHelper.java

public int searchHost(Host a) {

    List<Host> host = null;

    // create the query, but as a String
    String sql = "select * from Host where HOST_EMAIL like :email and HOST_NAME like :name";

    try {//from   www . 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(Host.class);

        q.setParameter("name", a.getHostName());
        q.setParameter("email", a.getHostEmail());

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

    return host.size();
}

From source file:MeetingApp.ParticipantHelper.java

public List getParticipants() {
    List<Participant> participantList = null;

    String sql = "select * from participant";

    try {/*  w w  w  .jav a  2 s . c  om*/
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);
        q.addEntity(Participant.class);

        participantList = (List<Participant>) q.list();

    } catch (Exception e) {
        e.printStackTrace();
        ;
    }

    return participantList;
}

From source file:MeetingApp.ParticipantHelper.java

public int insertParticipant(Participant a) {
    int result = 0;

    String sql = "insert into participant(PARTICIPANT_EMAIL, PARTICIPANT_NAME)" + "values (:email, :name)";

    try {//  ww w .ja  v a 2 s.  c  o  m
        // 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(Participant.class);

        // binding values to the placeholders in the query
        q.setParameter("name", a.getParticipantName());
        q.setParameter("email", a.getParticipantEmail());

        // 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:MeetingApp.ParticipantHelper.java

public int searchParticipant(Participant a) {

    List<Participant> participant = null;

    // create the query, but as a String
    String sql = "select * from Participant where PARTICIPANT_EMAIL like :email and PARTICIPANT_NAME like :name";

    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(Participant.class);

        q.setParameter("name", a.getParticipantName());
        q.setParameter("email", a.getParticipantEmail());

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

    return participant.size();
}

From source file:MeetingApp.ParticipantMeetingHelper.java

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

    String sql = "insert into participant_meeting(MEETING_ID, STATUS_ID, PARTICIPANT_EMAIL)"
            + "values (:meeting, :status, :participant)";

    try {/* ww w  .ja v a 2  s .co m*/
        // 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", num);
        q.setParameter("participant", em);
        q.setParameter("status", 3);

        // 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:MeetingApp.ParticipantMeetingHelper.java

public List getParticipants(int meetingId) {
    List<ParticipantMeeting> participantList = null;
    String sql = "select * from participant_meeting " + "where MEETING_ID = :meetingId";

    try {/*  w  ww . j  a v a  2s.  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);
        // bind values to the query placeholders
        q.setParameter("meetingId", meetingId);

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

    return participantList;
}

From source file:MeetingApp.ParticipantMeetingHelper.java

public List getMeetingName(int startID, String participantEmail) {

    List<ParticipantMeeting> meetingList = null;

    // create the query, but as a String `meetingdb`.`participant_meeting`
    // :start and :end, are placeholders for actual values
    // passed in as parameters and hard-coded
    String sql = "select * from participant_meeting " + "where PARTICIPANT_EMAIL = :participantEmail";

    try {/*w ww.j  a va 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);
        // bind values to the query placeholders
        //q.setParameter("start", startID);
        //q.setParameter("end", 5);
        q.setParameter("participantEmail", participantEmail);

        // 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;
}