Exemplo n.º 1
0
 /**
  * 根据查询HQL与参数列表创建Query对象.
  *
  * @param values 命名参数,按名称绑定.
  */
 public Query createQuery(final String hql, final Map<String, ?> values) {
   Assert.hasText(hql, "hql不能为空");
   Query query = getSession().createQuery(hql);
   if (values != null) {
     query.setProperties(values);
   }
   return query;
 }
Exemplo n.º 2
0
 /**
  * 合并对象
  *
  * @param entity
  * @return
  */
 public T merge(final T entity) {
   Assert.notNull(entity, "entity不能为空");
   Session session = getSession();
   String idName = getIdName();
   PropertyDescriptor idp = BeanUtils.getPropertyDescriptor(entityClass, idName);
   PK idvalue = null;
   try {
     idvalue = (PK) idp.getReadMethod().invoke(entity);
   } catch (Exception e) {
     throw new FatalBeanException("Could not copy properties from source to target", e);
   }
   T dest = null;
   if (idvalue != null) {
     dest = (T) session.get(entityClass, idvalue);
   }
   if (dest != null) {
     // merge the properties
     PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(entityClass);
     for (PropertyDescriptor p : descriptors) {
       if (p.getWriteMethod() != null) {
         try {
           Method readMethod = p.getReadMethod();
           if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
             readMethod.setAccessible(true);
           }
           Object value = readMethod.invoke(entity);
           if (value == null) {
             continue;
           }
           Method writeMethod = p.getWriteMethod();
           if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
             writeMethod.setAccessible(true);
           }
           writeMethod.invoke(dest, value);
         } catch (Throwable ex) {
           throw new FatalBeanException("Could not copy properties from source to target", ex);
         }
       }
     }
   } else {
     // destination object is empty, save the entity object parameted
     dest = entity;
   }
   session.saveOrUpdate(dest);
   logger.debug("merge entity: {}", entity);
   return dest;
 }
Exemplo n.º 3
0
 /**
  * 执行HQL进行批量修改/删除操作.
  *
  * @return 更新记录数.
  */
 public int batchExecute(final String hql, final Map<String, ?> values) {
   Assert.hasText(hql, "hql不能为空");
   return createQuery(hql, values).executeUpdate();
 }
Exemplo n.º 4
0
 /** 按属性查找唯一对象,匹配方式为相等. 查询结果最多有一行,超过一行会抛出异常 */
 public T findUniqueBy(final String propertyName, final Object value) {
   Assert.hasText(propertyName, "propertyName不能为空");
   Criterion criterion = Restrictions.eq(propertyName, value);
   return (T) createCriteria(criterion).uniqueResult();
 }
Exemplo n.º 5
0
 /** 按id获取对象. */
 public T get(final PK id) {
   Assert.notNull(id, "id不能为空");
   return (T) getSession().get(entityClass, id);
 }
Exemplo n.º 6
0
 /** 按id删除对象. */
 public void delete(final PK id) {
   Assert.notNull(id, "id不能为空");
   delete(get(id));
   logger.debug("delete entity {},id is {}", entityClass.getSimpleName(), id);
 }
Exemplo n.º 7
0
 /**
  * 删除对象.
  *
  * @param entity 对象必须是session中的对象或含id属性的transient对象.
  */
 public void delete(final T entity) {
   Assert.notNull(entity, "entity不能为空");
   getSession().delete(entity);
   logger.debug("delete entity: {}", entity);
 }
Exemplo n.º 8
0
 /** 保存新增或修改的对象. */
 public void save(final T entity) {
   Assert.notNull(entity, "entity不能为空");
   getSession().saveOrUpdate(entity);
   logger.debug("save entity: {}", entity);
 }