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:restaurantsearch.UserHelper.java

public Reservation getRestaurantDetails(int userId) {

    Reservation reservation = null;/*from  w w w  . j av a 2 s  . c o m*/

    String sql = "select * from reservation " + "where user_id = :id";

    try {

        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Reservation.class);

        q.setParameter("id", userId);

        reservation = (Reservation) q.uniqueResult();

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

    return reservation;
}

From source file:restaurantsearch.UserHelper.java

public int updateUser(String fName, String lName, String phone, String email, int userId) {
    int result = 0;

    String sql = "update user " + "set user_fname = :fName, user_lname = :lName, "
            + "user_phone = :phone, user_email = :email " + "where user_id = :userId";
    try {/*w w w  . ja v a  2 s . com*/
        // starting a transaction if one isn't active
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

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

        // binding values to the placeholders in the query
        q.setParameter("fName", fName);
        q.setParameter("lName", lName);
        q.setParameter("phone", phone);
        q.setParameter("email", email);
        q.setParameter("userId", userId);

        // 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:ru.runa.wfe.presentation.hibernate.HibernateCompilerQueryBuilder.java

License:Open Source License

/**
 * Builds {@link Query} for {@link BatchPresentation}.
 *
 * @return {@link Query} to load data from database.
 *///from   ww  w  .j a va 2s .  c  om
public Query build() {
    hqlBuilder.build();
    String sqlRequest = translateToSQL();
    if (parameters.isCountQuery() || parameters.isOnlyIdentityLoad()) {
        return session.createSQLQuery(sqlRequest).setResultTransformer(CountIdResultTransformer.INSTANCE);
    } else {
        SQLQuery query = session.createSQLQuery(sqlRequest);
        query.addEntity(batchPresentation.getType().getPresentationClass());
        return query;
    }
}

From source file:Serv.SQLProcess.java

public void eventsFromDB() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    String sql = "SELECT * FROM Events";
    SQLQuery query = session.createSQLQuery(sql);
    List<Events> results = query.addEntity(Events.class).list();
    System.out.println(results);//w  w w.  j a  va  2s.  co m

    ev = (ArrayList) results;

    session.close();
}

From source file:Serv.SQLProcess.java

public Boolean checkUniqueEvent(String title) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    String sql = "SELECT * FROM Events WHERE title='" + title + "'";
    SQLQuery query = session.createSQLQuery(sql);
    List<Events> results = new ArrayList<>();
    results = query.addEntity(Events.class).list();
    System.out.println(results);//w  ww .  j  a  v  a 2s .  co  m

    if (!results.isEmpty()) {
        session.close();

        return false;
    }
    //        for(int i = 0; i < results.size(); i++){
    //            if(results.get(i).getTitle().equals(title)){
    //                session.close();
    //                return false;
    //            }
    //        }

    session.close();
    return true;
}

From source file:Serv.SQLProcess.java

public Boolean checkUniqueQuestion(int eventId, String itemname) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    Events tempEvent = new Events();
    String sql = "SELECT * FROM Questions " // ?  
            + "WHERE itemname ='" + itemname + "'" + " AND idevent=" + eventId;
    SQLQuery query = session.createSQLQuery(sql);
    List<Questions> results = new ArrayList<>();
    results = query.addEntity(Questions.class).list();
    System.out.println(results);//from   w ww  .j a  v a 2 s .c om

    if (!results.isEmpty()) {
        session.close();

        return false;
    }

    session.close();
    return true;
}

From source file:Serv.SQLProcess.java

public void renameEvent(String oldTitle, String title) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from w w  w.  j  ava  2 s .  c om
    String sql = "SELECT * FROM Events WHERE title='" + oldTitle + "'";
    SQLQuery query = session.createSQLQuery(sql);
    List<Events> results = query.addEntity(Events.class).list();

    Events tempEvent = new Events();

    tempEvent = results.get(0);

    tempEvent.setTitle(title);

    session.update(tempEvent);

    session.getTransaction().commit();
    session.close();
}

From source file:Serv.SQLProcess.java

public void changeActive(int eventId, ArrayList<String> itemnames) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from ww w.  j a va  2s.c  o  m

    Events tempEvent = (Events) session.get(Events.class, eventId);

    String sql = "SELECT * FROM Questions " // ?  
            + "WHERE itemname <> 'Surname' " + "AND itemname <> 'Name' " + "AND idevent=" + eventId;
    SQLQuery query = session.createSQLQuery(sql);
    ArrayList<Questions> results = (ArrayList) query.addEntity(Questions.class).list();

    Questions question = new Questions();
    for (int i = 0; i < results.size(); i++) {
        question = results.get(i);

        if (itemnames.contains(question.getItemname())) {
            String sqlupdate = "UPDATE Questions SET isActive=1 WHERE idevent=" + eventId + " AND itemname = '"
                    + question.getItemname() + "'";
            query = session.createSQLQuery(sqlupdate);
            query.addEntity(Questions.class).executeUpdate();
        } else {
            String sqlupdate = "UPDATE Questions SET isActive=0 WHERE idevent=" + eventId + " AND itemname = '"
                    + question.getItemname() + "'";
            query = session.createSQLQuery(sqlupdate);
            query.addEntity(Questions.class).executeUpdate();
        }
        session.save(question);
    }

    session.getTransaction().commit();
    session.close();
}

From source file:Serv.SQLProcess.java

public void questionsFromDB(int eventId) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    String sql = "SELECT * FROM Questions " + "WHERE itemname <> 'Surname' " + "AND itemname <> 'Name' "
            + "AND idevent=" + eventId;
    SQLQuery query = session.createSQLQuery(sql);
    ArrayList<Questions> results = (ArrayList) query.addEntity(Questions.class).list();
    System.out.println(results);/*w w  w  .  ja  v  a 2  s.c o m*/

    questions = results;

    session.close();

}

From source file:Serv.SQLProcess.java

public QuestionWithAnswers oneQuestionFromDB(int eventId, String itemname) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    String sql = "SELECT * FROM Questions " + "WHERE itemname = '" + itemname + "'" + "AND idevent=" + eventId;
    SQLQuery query = session.createSQLQuery(sql);
    ArrayList<Questions> results = (ArrayList) query.addEntity(Questions.class).list();

    System.out.println(results);/*from   w  w w .jav  a  2 s .co  m*/

    Questions tempQ = results.get(0);

    sql = "SELECT * FROM Answers " + "WHERE iditem=" + tempQ.getIditem();
    query = session.createSQLQuery(sql);
    ArrayList<Answers> answers = (ArrayList) query.addEntity(Answers.class).list();

    QuestionWithAnswers qwa = new QuestionWithAnswers();

    qwa.setQuestion(tempQ);

    for (int i = 0; i < answers.size(); i++) {
        qwa.getAnswers().add(answers.get(i).getId().getAnswer());
    }

    session.close();

    return qwa;

}