public void save(Owner owner) throws DataAccessException {
   if (owner.isNew()) {
     hibernateTemplate.save(owner);
   } else {
     hibernateTemplate.update(owner);
   }
 }
Example #2
0
 public static void main(String[] args) {
   HibernateTemplate template = (HibernateTemplate) Util.getObject("template");
   final Person p1 = new Person();
   p1.setId(109);
   p1.setFirstName("vins");
   p1.setLastName("jains");
   p1.setAge(26);
   Object obj = template.save(p1);
   System.out.println("done with " + obj);
   System.out.println("done");
 }
 public Collection<Owner> findByLastName(String lastName) throws DataAccessException {
   List<Owner> owners =
       (List<Owner>)
           hibernateTemplate.find("from Owner o where o.last_name like ?", lastName + "%");
   System.out.println(owners);
   return owners;
 }
Example #4
0
  public List getListForPage(
      final Class arg,
      final Criterion[] criterions,
      final int offset,
      final int length,
      final String sortAttr,
      final String sortDir,
      final Projection _project) {

    List list =
        template.execute(
            new HibernateCallback() {
              public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = session.createCriteria(arg);
                criteria.setCacheable(true);
                for (int i = 0; i < criterions.length; i++) {
                  if (criterions[i] != null) criteria.add(criterions[i]);
                }
                if (sortDir != null && sortAttr != null) {
                  String[] dirs = sortDir.split(",");
                  String[] attrs = sortAttr.split(",");
                  for (int j = 0; j < attrs.length; j++) {
                    if (!(dirs[j] == null || dirs[j].length() == 0))
                      if (dirs[j].equals("ASC")) criteria.addOrder(Order.asc(attrs[j]));
                      else criteria.addOrder(Order.desc(attrs[j]));
                  }
                }
                if (_project != null) criteria.setProjection(_project);
                criteria.setFirstResult(offset);
                criteria.setMaxResults(length);
                return criteria.list();
              }
            });
    return list;
  }
Example #5
0
 @Override
 @Transactional
 public void saveStock(Stock stock) {
   System.out.println("sessionFactory: " + sessionFactory);
   //        getSessionFactory().openSession();
   //        getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
   // System.out.println("Get Current Session:"+getSessionFactory().openSession());
   hibernateTemplate.save(stock);
 }
Example #6
0
 public User validate(User user) {
   Session session = hibernateTemplate.getSessionFactory().openSession();
   Criteria criteria =
       session
           .createCriteria(User.class)
           .add(Restrictions.eq("studentId", user.getStudentId()))
           .add(Restrictions.eq("password", user.getPassword()));
   List<User> list = criteria.list();
   session.close();
   return list.get(0);
 }
 // 查询
 @Test
 public void test_get() {
   // SessionFactory sessionFactory =
   // HibernateUtils.getSessionFactory();//方式一:没有整合Spring+Hibernate时,需要使用hibernate.cfg.xml手动创建
   SessionFactory sessionFactory =
       hibernateTemplate.getSessionFactory(); // 方式二:整合Spring+Hibernate成功后,可以从Spring容器中直接获取Bean
   Session session = sessionFactory.openSession();
   // Transaction tx = session.beginTransaction();
   Customer customer = (Customer) session.get(Customer.class, 1);
   // tx.commit();
   session.close();
   System.out.println(customer);
 }
  // 添加
  @Test
  public void test_saveOrUpdate1() {
    Customer customer = new Customer();
    customer.setName("monday#test_saveOrUpdate1");

    // SessionFactory sessionFactory =
    // HibernateUtils.getSessionFactory();//方式一:没有整合Spring+Hibernate时,需要使用hibernate.cfg.xml手动创建
    SessionFactory sessionFactory =
        hibernateTemplate.getSessionFactory(); // 方式二:整合Spring+Hibernate成功后,可以从Spring容器中直接获取Bean
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.saveOrUpdate(customer); // OID 为空 执行save操作
    tx.commit();
    session.close();
  }
  // 删除
  // 因为与其他测试方法存在冲突,需要独立测试(请先注释其他测试方法),测试才会通过
  @Test
  public void test_delete() {
    Customer customer = new Customer();
    customer.setId(5);

    // SessionFactory sessionFactory =
    // HibernateUtils.getSessionFactory();//方式一:没有整合Spring+Hibernate时,需要使用hibernate.cfg.xml手动创建
    SessionFactory sessionFactory =
        hibernateTemplate.getSessionFactory(); // 方式二:整合Spring+Hibernate成功后,可以从Spring容器中直接获取Bean
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.delete(customer);
    tx.commit();
    session.close();
  }
 // 删除
 @Test
 public void test_delete_hql() {
   String hql = "delete from Customer where id=?";
   // SessionFactory sessionFactory =
   // HibernateUtils.getSessionFactory();//方式一:没有整合Spring+Hibernate时,需要使用hibernate.cfg.xml手动创建
   SessionFactory sessionFactory =
       hibernateTemplate.getSessionFactory(); // 方式二:整合Spring+Hibernate成功后,可以从Spring容器中直接获取Bean
   Session session = sessionFactory.openSession();
   Transaction tx = session.beginTransaction();
   Query query = session.createQuery(hql);
   query.setParameter(0, 6);
   query.executeUpdate();
   tx.commit();
   session.close();
 }
  // 添加
  @Test
  public void test_save() {

    Customer customer = new Customer();
    customer.setName("monday#test_save");

    // SessionFactory sessionFactory =
    // HibernateUtils.getSessionFactory();//方式一:没有整合Spring+Hibernate时,需要使用hibernate.cfg.xml手动创建
    SessionFactory sessionFactory =
        hibernateTemplate.getSessionFactory(); // 方式二:整合Spring+Hibernate成功后,可以从Spring容器中直接获取Bean
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    Serializable id = session.save(customer); // 返回OID
    tx.commit();
    session.close();
    System.out.println("id=" + id);
  }
Example #12
0
  public List getListForPage(final String hql, final int offset, final int lengh) {
    try {
      List list =
          template.execute(
              new HibernateCallback() {

                public Object doInHibernate(Session session) throws HibernateException {
                  List list2 =
                      session.createQuery(hql).setFirstResult(offset).setMaxResults(lengh).list();
                  return list2;
                }
              });
      return list;
    } catch (RuntimeException re) {

      throw re;
    }
  }
Example #13
0
  public List getRowCnt(final Class arg, final String cattr, final Criterion[] criterions) {

    List list =
        template.execute(
            new HibernateCallback() {
              public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = session.createCriteria(arg);
                for (int i = 0; i < criterions.length; i++) {
                  if (criterions[i] != null) criteria.add(criterions[i]);
                }

                // criteria.setProjection(Projections.rowCount());
                criteria.setProjection(Projections.count(cattr));
                return criteria.list(); // ((Long)
                // criteria.uniqueResult()).intValue();
              }
            });
    return list;
  }
Example #14
0
  public List getCachedQuery(final String hql) {
    try {
      List list =
          template.execute(
              new HibernateCallback() {

                public Object doInHibernate(Session session) throws HibernateException {
                  List list2 =
                      session
                          .createQuery(hql)
                          .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
                          .setCacheable(true)
                          .list();
                  return list2;
                }
              });
      return list;
    } catch (RuntimeException re) {

      throw re;
    }
  }
Example #15
0
 public void studentDelete(Student student) {
   hibernateTemplate.delete(student);
 }
Example #16
0
 public void studentUpdate(Student student) {
   hibernateTemplate.update(student);
 }
Example #17
0
 @Transactional
 public void studentSave(Student student) {
   hibernateTemplate.save(student);
 }
Example #18
0
 @Override
 public Person findPerson(int id) {
   List<Object> list = hibernateTemplate.find(" from Person where id=?", id);
   return (Person) list.get(0);
 }
Example #19
0
 public Campus findCampus(int id) {
   List<Object> list = hibernateTemplate.find(" from Campus where id=?", id);
   return (Campus) list.get(0);
 }
Example #20
0
 public Course findCourse(int id) {
   List<Object> list = hibernateTemplate.find(" from Course where course_id=?", id);
   return (Course) list.get(0);
 }
Example #21
0
 public void courseUpdate(Course course) {
   hibernateTemplate.update(course);
 }
Example #22
0
 @Transactional
 public Student findStudent(int id) {
   List<Object> list = hibernateTemplate.find(" from Student where student_Id=?", id);
   return (Student) list.get(0);
 }
Example #23
0
 @Override
 public void deleteStock(Stock stock) {
   hibernateTemplate.delete(stock);
 }
Example #24
0
 @Override
 public void updateStock(Stock stock) {
   hibernateTemplate.update(stock);
 }
Example #25
0
 public void courseSave(Course course) {
   hibernateTemplate.save(course);
 }
Example #26
0
 @Transactional
 public void saveObject(Object obj) {
   hibernateTemplate.save(obj);
 }
Example #27
0
 public void courseDelete(Course course) {
   hibernateTemplate.delete(course);
 }
Example #28
0
 @Override
 @Transactional(readOnly = false)
 public void save(Person person) {
   hibernateTemplate.save(person);
 }
Example #29
0
 @Override
 public Stock findByStockCode(String stockCode) {
   List list = hibernateTemplate.find("from Stock where stockCode=?", stockCode);
   return (Stock) list.get(0);
 }
Example #30
0
 @Override
 @Transactional
 public void delete(Person person) {
   hibernateTemplate.delete(person);
 }