Java tutorial
package com.carmanage.dao.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import com.carmanage.dao.IBusDao; import com.carmanage.domain.Car; import com.carmanage.util.PageController; @Component public class BusDaoImpl implements IBusDao { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void deleteBusByString(String plate) { Session session = sessionFactory.openSession(); Query query = session.createQuery("delete from Car e where e.plate=?"); query.setString(0, plate); query.executeUpdate(); session.flush(); session.close(); } public List<Object[]> getBrand() { Session session = sessionFactory.openSession(); Query query = session.createQuery("select distinct e.brand from Car e"); List<Object[]> brand = query.list(); session.close(); return brand; } public List<Object[]> getInsurance_date() { Session session = sessionFactory.openSession(); Query query = session.createQuery("select distinct e.insurance_date from Car e"); List<Object[]> insurance_date = query.list(); session.close(); return insurance_date; } public List<Object[]> getPlate() { Session session = sessionFactory.openSession(); Query query = session.createQuery("select distinct e.plate from Car e"); List<Object[]> plate = query.list(); session.close(); return plate; } public List<Object[]> getRegister_date() { Session session = sessionFactory.openSession(); Query query = session.createQuery("select distinct e.register_date from Car e"); List<Object[]> register_date = query.list(); session.close(); return register_date; } public List<Object[]> getSeat() { Session session = sessionFactory.openSession(); Query query = session.createQuery("select distinct e.seat from Car e"); List<Object[]> seat = query.list(); session.close(); return seat; } public int getTotalRows() { int totalRow = 0; Session session = sessionFactory.openSession(); Query query = session.createQuery("select count(e) from Car e"); Object oj = query.list().get(0); Number num = (Number) oj; totalRow = num.intValue(); session.close(); return totalRow; } public List<Car> groupSearch(Car bus, PageController pc) { String plate = bus.getPlate(); String brand = bus.getBrand(); int seat = bus.getSeat(); String register_date = bus.getRegister_date(); String insurance_date = bus.getInsurance_date(); Session session = sessionFactory.openSession(); Query query = session .createQuery("from Car e where e.plate=? and e.brand=? and " + "e.seat=? and e.register_date=? and e.insurance_date=?") .setFirstResult(pc.getPageStartRow()).setMaxResults(pc.getPageSize()); query.setString(0, plate); query.setString(1, brand); query.setInteger(2, seat); query.setString(3, register_date); query.setString(4, insurance_date); List<Car> buses = query.list(); session.close(); return buses; } public int getGroupTotalRows(Car bus) { String plate = bus.getPlate(); String brand = bus.getBrand(); int seat = bus.getSeat(); String register_date = bus.getRegister_date(); String insurance_date = bus.getInsurance_date(); int totalRow = 0; Session session = sessionFactory.openSession(); Query query = session.createQuery("select count(e) from Car e where e.plate=? and e.brand=? and " + "e.seat=? and e.register_date=? and e.insurance_date=?"); query.setString(0, plate); query.setString(1, brand); query.setInteger(2, seat); query.setString(3, register_date); query.setString(4, insurance_date); Object oj = query.list().get(0); Number num = (Number) oj; totalRow = num.intValue(); session.close(); return totalRow; } public void save(Car bus) { Session session = sessionFactory.openSession(); session.save(bus); session.flush(); session.close(); } public List<Car> showAllBuses(PageController pc) { Session session = sessionFactory.openSession(); Query query = session.createQuery("from Car").setFirstResult(pc.getPageStartRow()) .setMaxResults(pc.getPageSize()); List<Car> Buses = query.list(); session.close(); return Buses; } public Car showModifyData(String modifyBusPlate) { Session session = sessionFactory.openSession(); Query query = session.createQuery("select e from Car e where e.plate=?"); query.setString(0, modifyBusPlate); Car car = (Car) query.uniqueResult(); session.close(); return car; } public void modiCar(Car car) { String plate = car.getPlate(); String brand = car.getBrand(); int seat = car.getSeat(); String register_date = car.getRegister_date(); String insurance_date = car.getInsurance_date(); String driver_license = car.getDriver_license(); String driving_license = car.getDriving_license(); Session session = sessionFactory.openSession(); String hql = "Update Car e set e.brand=?,e.seat=?,e.register_date=?,e.insurance_date=?,e.driver_license=?,e.driving_license=? where e.plate=?"; Query query = session.createQuery(hql); query.setString(0, brand); query.setInteger(1, seat); query.setString(2, register_date); query.setString(3, insurance_date); query.setString(4, driver_license); query.setString(5, driving_license); query.setString(6, plate); query.executeUpdate(); session.flush(); session.close(); } }