List of usage examples for org.hibernate SQLQuery addEntity
SQLQuery<T> addEntity(Class entityType);
From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4PersistenceDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from ww w . j a va 2s. co m*/ public <R> List<R> findByNativeQuery(final Class<R> resultClass, final String sql, final Object[] values, final int start, final int maxRows) { SQLQuery query = getCurrentSession().createSQLQuery(sql); query.addEntity(resultClass); setQueryParameters(query, values, start, maxRows); return query.list(); }
From source file:OTS.DataModels.MySqlDataSource.java
@Override public void ExecuteDataSet(String sql, List<Object> entities, Object[] list) { Session session = null;//from w w w . j a v a2s. c o m try { sessionFactory = NewHibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); SQLQuery query = session.createSQLQuery(sql); for (Object a : entities) { query.addEntity(a.getClass()); } query.list().toArray(list); } finally { if (session != null) { session.close(); } } }
From source file:pkg.NativeSqlProductCode.java
public void listEmployeesEntity() { Session session = sessionFactory.openSession(); Transaction tx = null;/*w ww . ja v a2s. c o m*/ try { tx = session.beginTransaction(); String sql = "SELECT * FROM EMPLOYEE"; SQLQuery query = session.createSQLQuery(sql); query.addEntity(ProductCode.class); List employees = query.list(); for (Iterator iterator = employees.iterator(); iterator.hasNext();) { ProductCode pc = (ProductCode) iterator.next(); System.out.print("Product Code: " + pc.getProdCode() + " Discount Code: " + pc.getDiscountCode()); System.out.println(" Description: " + pc.getDescription()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } }
From source file:products.OrdersHelper.java
public static List getAllByUser(String user) { Session session = null;/* w w w . ja v a2s . c o m*/ List list = null; try { session = NewHibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); String sql = "SELECT * FROM orders WHERE user = :username"; SQLQuery query = session.createSQLQuery(sql); query.addEntity(Orders.class); query.setParameter("username", user); list = query.list(); System.out.println("list:" + list.size()); // list = session.createQuery(query).list(); session.close(); } catch (Exception e) { System.out.println("OrdersHelper:" + e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } return list; } }
From source file:project.dao.ShareCourseDAO.java
/** * admin and user/*w w w . j a v a 2s . co m*/ * * @param idaccount * @param idcourse * @return */ //lay share course theo id course v id account admin create course public static ShareCourse getShareCouseByIdAccountAndIdCourseAdmin(int idaccount, int idcourse) { ShareCourse result = null; Session session = HibernateUtil.getSessionFactory().openSession(); //Session session = new Configuration().configure().buildSessionFactory().openSession(); Transaction tx = null; try { tx = session.beginTransaction(); SQLQuery query = session.createSQLQuery(SQL.GET_SHARE_COURSE_BY_IDACCOUNT_AND_IDCOURSE); query.addEntity(ShareCourse.class); query.setParameter("id_course", idcourse); query.setParameter("id_account_share", idaccount); query.setParameter("id_account_use", idaccount); query.setParameter("id_account_create", idaccount); List list = query.list(); if (list != null && list.size() > 0) { result = (ShareCourse) list.get(0); } tx.commit(); } catch (NullPointerException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } return result; }
From source file:restaurantsearch.CuisineHelper.java
public List<Restaurant> selectRestaurantByCuisine(String keyWord) { List<Restaurant> restaurantList = null; String sql = " select * from cuisine, restaurant " + "where cuisine_type like :keyWord" + " and cuisine.cuisine_id = restaurant.cuisine_id"; try {//from w ww . ja va 2 s . c o m // starting a transaction if one isn't active if (!this.session.getTransaction().isActive()) { session.beginTransaction(); } SQLQuery q = session.createSQLQuery(sql); q.addEntity(Restaurant.class); q.setParameter("keyWord", keyWord); restaurantList = (List<Restaurant>) q.list(); } catch (Exception e) { e.printStackTrace(); } return restaurantList; }
From source file:restaurantsearch.CuisineHelper.java
public Restaurant getRestaurantDetails(int restaurantId) { Restaurant restaurant = null;//from ww w . j a v a2s . 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", restaurantId); restaurant = (Restaurant) q.uniqueResult(); } catch (Exception e) { e.printStackTrace(); } return restaurant; }
From source file:restaurantsearch.LocationHelper.java
public List<Restaurant> selectRestaurantByLocation(String keyWord) { List<Restaurant> restaurantList = null; String sql = " select * from location, restaurant " + "where location_name like :keyWord" + " and location.location_id = restaurant.location_id"; try {/*ww w .ja v a 2s .co m*/ // starting a transaction if one isn't active if (!this.session.getTransaction().isActive()) { session.beginTransaction(); } SQLQuery q = session.createSQLQuery(sql); q.addEntity(Restaurant.class); q.setParameter("keyWord", keyWord); restaurantList = (List<Restaurant>) q.list(); } catch (Exception e) { e.printStackTrace(); } return restaurantList; }
From source file:restaurantsearch.ReservationHelper.java
public int insertReservation(Date date, String time, int numGuests, int userId, int restId) { int result = 0; String sql = "insert into reservation(reservation_date, reservation_time, reservation_num_guests, " + "user_id, resaurant_id) " + "values (:date, :time, :numGuests, :userId, :restId)"; try {/*from w w w . j av a2s . 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); // 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.ReservationHelper.java
public List getRestaurant() { // setting local variable that will be used to return list // of lanuages List<Restaurant> restaurantList = null; // creating query as a String String sql = "select * from restaurant"; try {/* ww w . ja va 2 s . 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(Restaurant.class); // executing the query restaurantList = (List<Restaurant>) q.list(); } catch (Exception e) { e.printStackTrace(); } return restaurantList; }