public boolean remove(Class<?> clazz, String attr, Object... values) { if (clazz == null || Strings.isEmpty(attr) || values == null || values.length == 0) { return false; } String entityName = modelMeta.getEntityType(clazz).getEntityName(); StringBuilder hql = new StringBuilder(); hql.append("delete from ").append(entityName).append(" where ").append(attr).append(" in (:ids)"); Map<String, Object> parameterMap = CollectUtils.newHashMap(); parameterMap.put("ids", values); return executeUpdateHql(hql.toString(), parameterMap) > 0; }
public boolean remove(Class<?> clazz, Map<String, Object> keyMap) { if (clazz == null || keyMap == null || keyMap.isEmpty()) { return false; } String entityName = modelMeta.getEntityType(clazz).getEntityName(); StringBuilder hql = new StringBuilder(); hql.append("delete from ").append(entityName).append(" where "); Set<String> keySet = keyMap.keySet(); Map<String, Object> params = CollectUtils.newHashMap(); for (final String keyName : keySet) { Object keyValue = keyMap.get(keyName); String paramName = keyName.replace('.', '_'); params.put(paramName, keyValue); if (keyValue.getClass().isArray() || keyValue instanceof Collection<?>) { hql.append(keyName).append(" in (:").append(paramName).append(") and "); } else { hql.append(keyName).append(" = :").append(paramName).append(" and "); } } hql.append(" (1=1) "); return (executeUpdateHql(hql.toString(), params) > 0); }
@SuppressWarnings("unchecked") public <T> List<T> get(Class<T> entityClass, String keyName, Collection<?> values) { if (entityClass == null || Strings.isEmpty(keyName) || values == null || values.isEmpty()) { return Collections .emptyList(); } String entityName = modelMeta.getEntityType(entityClass).getEntityName(); return (List<T>) get(entityName, keyName, values.toArray()); }
@SuppressWarnings("unchecked") public <T> List<T> getAll(Class<T> clazz) { String hql = "from " + modelMeta.getEntityType(clazz).getEntityName(); Query query = getSession().createQuery(hql); query.setCacheable(true); return query.list(); }
private void saveEntity(Object entity, String entityName) { if (null == entity) return; if (null != entityName) { getSession().save(entityName, entity); } else { if (entity instanceof HibernateProxy) { getSession().save(entity); } else { getSession().save(modelMeta.getEntityType(entity.getClass()).getEntityName(), entity); } } }
@SuppressWarnings({ "unchecked" }) public <T> T get(Class<T> clazz, Serializable id) { return (T) get(modelMeta.getEntityType(clazz).getEntityName(), id); }
@SuppressWarnings("unchecked") public <T> List<T> get(Class<T> entityClass, String keyName, Object... values) { if (entityClass == null || Strings.isEmpty(keyName) || values == null || values.length == 0) { return Collections .emptyList(); } String entityName = modelMeta.getEntityType(entityClass).getEntityName(); return (List<T>) get(entityName, keyName, values); }
private void loggerTypeInfo() { List<String> names = CollectUtils.newArrayList(entityTypes.keySet()); Collections.sort(names); for (final String entityName : names) { EntityType entityType = (EntityType) entityTypes.get(entityName); logger.debug("entity:{}-->{}", entityType.getEntityName(), entityType.getEntityClass().getName()); logger.debug("propertyType size:{}", Integer.valueOf(entityType.getPropertyTypes().size())); } names.clear(); names.addAll(collectionTypes.keySet()); Collections.sort(names); for (final String collectionName : names) { CollectionType collectionType = (CollectionType) collectionTypes.get(collectionName); logger.debug("collection:{}", collectionType.getName()); logger.debug("class:{}", collectionType.getCollectionClass()); logger.debug("elementType:{}", collectionType.getElementType().getReturnedClass()); } }
@Override public String getEntityName(Object target) { if (target instanceof HibernateProxy) { return ((HibernateProxy) target).getHibernateLazyInitializer().getEntityName(); } else { return getEntityType(target.getClass()).getEntityName(); } }
protected Entity getEntity(HttpServletRequest request, Class entityClass, String name) { Long entityId = getLong(request, name + ".id"); if (null == entityId) { entityId = getLong(request, name + "Id"); } Entity entity = null; try { EntityType type = Model.getType(entityClass); if (null == entityId) { entity = (Entity) populate(request, type.newInstance(), type, name); } else { entity = (Entity) entityDao.get(type.getEntityName(), entityId); } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return entity; }
private void persistEntity(Object entity, String entityName) { if (null == entity) return; if (null != entityName) { getSession().saveOrUpdate(entityName, entity); } else { if (entity instanceof HibernateProxy) { getSession().saveOrUpdate(entity); } else { getSession().saveOrUpdate(modelMeta.getEntityType(entity.getClass()).getEntityName(), entity); } } }
@SuppressWarnings("unchecked") public <T> List<T> unmarshal(Dimension field, String text) { if (null == field.getTypeName()) { return (List<T>) CollectUtils.newArrayList(Strings.split(text, ",")); } else { Class<?> clazz = null; try { clazz = Class.forName(field.getTypeName()); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } EntityType myType = Model.getType(clazz); OqlBuilder<T> builder = OqlBuilder.from(myType.getEntityName(), "field"); String[] ids = Strings.split(text, ","); Class<?> propertyType = PropertyUtils.getPropertyType(clazz, field.getKeyName()); List<Object> realIds = CollectUtils.newArrayList(ids.length); for (String id : ids) { Object realId = ConvertUtils.convert(id, propertyType); realIds.add(realId); } builder.where("field." + field.getKeyName() + " in (:ids)", realIds).cacheable(); return entityDao.search(builder); } }