demoiselle / framework

Repositório principal contendo o Core e Extensions: JPA, Security, WS
https://demoiselle.org
127 stars 77 forks source link

Melhoria #104

Open egrohs opened 6 years ago

egrohs commented 6 years ago

Estou fazendo uns testes com demoiselle 3. Notei que o método mergeHalf (baseado em PATCH) do AbstractDAO parece incompleto e errado.

1) ta invocando TUDO, os gets (o que é correto) e os sets tb (o que é errado) 2) linha 76, ta passando erradamente a classe entityClass ao invés da instância entity

Abaixo segue uma implementação sugerida que vai direto no field com mapped column...

try {
            StringBuilder sb = new StringBuilder();
            sb.append("UPDATE ");
            sb.append(entity.getClass().getCanonicalName());
            sb.append(" SET ");
            for (Field method : entity.getClass().getDeclaredFields()) {
                method.setAccessible(true);
                if (method.isAnnotationPresent(Column.class)) {
                    Object obj = method.get(entity);
                    if (obj != null) {
                        sb.append(method.getName()).append(" = ").append(obj).append(",");
                    }
                }
            }
            if (sb.toString().endsWith(",")) {
                int i = sb.lastIndexOf(",");
                sb.delete(i, i + 1);
                // TODO ainda falta testar chaves multiplas com @EmbeddedId
                sb.append(" WHERE ").append(CrudUtilHelper.getMethodAnnotatedWithID(entity.getClass())).append(" = ")
                        .append(id);
                getEntityManager().createQuery(sb.toString()).executeUpdate();
            }
            return entity;
        } catch (Exception e) {
            throw new DemoiselleCrudException("Não foi possível salvar", e);
        }

Att. Emanuel Motta Grohs

[UPDATE - Merlin]: apenas formatei o código pra ficar mais amigável ;)