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; }
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()); } }
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; }
entityType = Model.getType(clazz.getName()); if (clazz.isInterface()) { clazz = entityType.getEntityClass();
return null; entityType = new EntityType(cm.getEntityName(), cm.getMappedClass(EntityMode.POJO), cm.getIdentifierPropertyName()); entityTypes.put(cm.getEntityName(), entityType); Map<String, Type> propertyTypes = entityType.getPropertyTypes(); String[] ps = cm.getPropertyNames(); for (int i = 0; i < ps.length; i++) {
public static Object populate(HttpServletRequest request, Class clazz, String name) { EntityType type = null; if (clazz.isInterface()) type = Model.getType(clazz.getName()); else type = Model.getType(clazz); return populate(request, type.newInstance(), type, name); }
private ComponentType buildComponentType(SessionFactory sessionFactory, String entityName, String propertyName) { EntityType entityType = (EntityType) entityTypes.get(entityName); if (null != entityType) { Type propertyType = (Type) entityType.getPropertyTypes().get(propertyName); if (null != propertyType) { return (ComponentType) propertyType; } } ClassMetadata cm = sessionFactory.getClassMetadata(entityName); org.hibernate.type.ComponentType hcType = (org.hibernate.type.ComponentType) cm .getPropertyType(propertyName); String[] propertyNames = hcType.getPropertyNames(); ComponentType cType = new ComponentType(hcType.getReturnedClass()); Map<String, Type> propertyTypes = cType.getPropertyTypes(); for (int j = 0; j < propertyNames.length; j++) { org.hibernate.type.Type type = cm.getPropertyType(propertyName + "." + propertyNames[j]); if (type.isEntityType()) { propertyTypes.put(propertyNames[j], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(propertyNames[j], buildComponentType(sessionFactory, entityName, propertyName + "." + propertyNames[j])); } else if (type.isCollectionType()) { propertyTypes.put( propertyNames[j], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + propertyName + "." + propertyNames[j])); } } return cType; }
private CollectionType buildCollectionType(SessionFactory sessionFactory, Class<?> collectionClass, String role) { CollectionMetadata cm = sessionFactory.getCollectionMetadata(role); // FIXME buildCollectionType if (null == cm) return null; org.hibernate.type.Type type = cm.getElementType(); EntityType elementType = null; if (type.isEntityType()) { elementType = (EntityType) entityTypes.get(type.getName()); if (null == elementType) elementType = buildEntityType(sessionFactory, type.getName()); } else { elementType = new EntityType(type.getReturnedClass()); } CollectionType collectionType = new CollectionType(); collectionType.setElementType(elementType); collectionType.setArray(cm.isArray()); collectionType.setCollectionClass(collectionClass); if (!collectionTypes.containsKey(collectionType.getName())) { collectionTypes.put(collectionType.getName(), collectionType); } return collectionType; }
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()); }
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> List<T> getAll(Class<T> clazz) { String hql = "from " + modelMeta.getEntityType(clazz).getEntityName(); Query query = getSession().createQuery(hql); query.setCacheable(true); return query.list(); }
@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); }
@Override public String getEntityName(Object target) { if (target instanceof HibernateProxy) { return ((HibernateProxy) target).getHibernateLazyInitializer().getEntityName(); } else { return getEntityType(target.getClass()).getEntityName(); } }
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); } }