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.ReservationHelper.java

public List getUser() {

    // setting local variable that will be used to return list
    // of lanuages
    List<User> userList = null;

    // creating query as a String
    String sql = "select * from user";

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

        // creating actual query that will be executed against the database
        SQLQuery q = session.createSQLQuery(sql);

        // associating the Language POJO and table with the query
        q.addEntity(User.class);

        // executing the query
        userList = (List<User>) q.list();

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

    return userList;
}

From source file:restaurantsearch.ReservationHelper.java

public List<Reservation> getReservationDetailsByUserId(int userId) {

    List<Reservation> reservationList = null;

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

    try {/*from  w w  w .  j  a  v  a2 s .  c  om*/

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

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Reservation.class);

        q.setParameter("id", userId);

        reservationList = (List<Reservation>) q.list();

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

    return reservationList;
}

From source file:restaurantsearch.ReservationHelper.java

public Reservation getReservationDetailsByResId(int resId) {

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

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

    try {

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

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Reservation.class);

        q.setParameter("id", resId);

        reservation = (Reservation) q.uniqueResult();

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

    return reservation;
}

From source file:restaurantsearch.ReservationHelper.java

public int deleteReservationDetailsByresId(int resId) {

    int result = 0;

    String sql = "delete from reservation " + "where reservation_id = :resId";

    try {//  ww  w.  ja va  2s . c om

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

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Reservation.class);

        // binding values to the placeholders in the query
        q.setParameter("resId", resId);

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

        session.getTransaction().commit();

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

    return result;
}

From source file:restaurantsearch.ReservationHelper.java

public int updateReservation(Date date, String time, int numGuests, int userId, int restId, int reservationId) {
    int result = 0;

    String sql = "update reservation set reservation_date = :date, "
            + "reservation_time = :time, reservation_num_guests = :numGuests, "
            + "user_id = :userId, resaurant_id = :restId " + "where reservation_id = :reservationId";
    try {//w ww .j a v a  2  s  .c  o m
        // 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);

        q.addEntity(Reservation.class);

        // binding values to the placeholders in the query
        q.setParameter("date", date);
        q.setParameter("time", time);
        q.setParameter("numGuests", numGuests);
        q.setParameter("userId", userId);
        q.setParameter("restId", restId);
        q.setParameter("reservationId", reservationId);

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

public List<Restaurant> selectRestaurant(String keyWord) {
    List<Restaurant> restaurantList = null;

    String sql = "select * from restaurant " + "where restaurant_name like '%" + keyWord + "%'";

    try {// w w  w .j ava2s  . c o m
        //starting the transaction if one is not active
        if (!this.session.getTransaction().isActive()) {
            session.beginTransaction();
        }

        //creaing query that can be executed
        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Restaurant.class);

        //binding the values to the placeholders in the query
        //q.setParameter("keyWord", keyWord);

        //executing the query
        restaurantList = (List<Restaurant>) q.list();

        //commiting the query to the database
        session.getTransaction().commit();

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

    return restaurantList;
}

From source file:restaurantsearch.RestaurantHelper.java

public Restaurant getRestaurantDetails(int restId) {

    Restaurant restaurant = null;//  w w w .  ja v a  2  s  . c  om

    String sql = "select * from restaurant " + "where restaurant_id = :id";

    try {

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

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(Restaurant.class);

        q.setParameter("id", restId);

        restaurant = (Restaurant) q.uniqueResult();

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

    return restaurant;
}

From source file:restaurantsearch.UserHelper.java

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

    String sql = "insert into user(user_fname, user_lname, user_phone, " + "user_email) "
            + "values (:fName, :lName, :phone, :email)";
    try {//from  w  ww .j  a  v  a 2 s.  c  o  m
        // 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);

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

public User selectUser(String email) {
    User current = null;/* ww  w.j a  va  2  s . c o  m*/

    String sql = "select * from user where user_email = :email";

    try {

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

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(User.class);

        q.setParameter("email", email);

        current = (User) q.uniqueResult();

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

    return current;
}

From source file:restaurantsearch.UserHelper.java

public User selectUserById(int userId) {
    User current = null;// www .  j  a  va  2 s  .  c  om

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

    try {

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

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity(User.class);

        q.setParameter("id", userId);

        current = (User) q.uniqueResult();

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

    return current;
}