我如何将属性值从一个对象传输到另一个对象

How I can transfer atrributes values from a Objects to another

我想将来自实体管理器的对象的属性值转移到新对象。

return 对象始终为 null

 public class ReflectionUtil {

    public static Object copyAttributesFromTo(Object a, Object b) throws IllegalArgumentException, IllegalAccessException {
        Field[] fieldsFromFirstClass = a.getClass().getDeclaredFields();
        Field[] fieldsFromSecondClass = b.getClass().getDeclaredFields();

        for (Field currentFieldFromTheFirstClass : fieldsFromFirstClass) {
            for (Field currentFieldFromTheSecondClass : fieldsFromSecondClass) {
                String nameOfTheFirstField = currentFieldFromTheFirstClass.getName();
                String nameOfTheSecondField = currentFieldFromTheSecondClass.getName();

                if (!Modifier.isFinal(currentFieldFromTheFirstClass.getModifiers())) {//Dispensa os Final
                    if (!currentFieldFromTheFirstClass.isAnnotationPresent(Id.class)) {//Não sobescreve campo id
                        if (nameOfTheFirstField.equals(nameOfTheSecondField)) {
                            currentFieldFromTheFirstClass.setAccessible(true);
                            currentFieldFromTheSecondClass.setAccessible(true);
currentFieldFromTheSecondClass.get(b));
                            currentFieldFromTheFirstClass.set(a, currentFieldFromTheSecondClass.get(b));
                        }
                    }
                }
            }
        }

        return a;
    }
}

在 Facade 调用中,我总是必须将所有属性值放入新对象

 public void update(Profile object) {
        dao.beginTransaction();
        Profile persistedObject = dao.find(object.getId());
        persistedObject.setName(object.getName());
        dao.commitAndCloseTransaction();
    }

所以我想创建一些类似的东西

public void update(Profile object) {
            dao.beginTransaction();
            Profile persistedObject = dao.find(object.getId());
            ReflectionUtil.copyAttributesFromTo(persistedObject , object);
            dao.commitAndCloseTransaction();
        }

The return Object is always null

return 对象(即当您 returna 包含的任何内容)不可能是 null

很容易看出代码没有改变引用a。方法中没有赋值给它,所以它不能改变。

另一种可能性是您使用 anull 值调用该方法。但是如果你这样做,方法的第一行调用 a.getClass() 并且如果 anull.

将抛出 NPE

TL;DR - 这是不可能的。


那么这是什么意思呢?

以下是最可能的解释:

  1. 您误认为 null 正在 returned。也许没有调用该方法?也许,它不是returning?

  2. 也许您以其他方式误解了证据。不看代码就很难知道...和证据。

  3. 也许您的意思该方法是return对象;即我误解了这个问题。 (虽然你的问题描述很明确......)

我更改了代码并开始为我更新

 public static Object copyAttributesFromTo(Object a, Object b) throws IllegalArgumentException, IllegalAccessException {
        Field[] fieldsFromFirstClass = a.getClass().getDeclaredFields();
        Field[] fieldsFromSecondClass = b.getClass().getDeclaredFields();
        //JSFMessageUtil.addMsgLog(JSFMessageUtil.matricula, ReflectionUtil.class.getCanonicalName(), "ReflectionUtil: Aqui");
        for (Field currentFieldFromTheFirstClass : fieldsFromFirstClass) {
            for (Field currentFieldFromTheSecondClass : fieldsFromSecondClass) {
                Object nameOfTheFirstField = currentFieldFromTheFirstClass.getName();
                Object nameOfTheSecondField = currentFieldFromTheSecondClass.getName();

                if (!Modifier.isFinal(currentFieldFromTheFirstClass.getModifiers())) {//Dispensa os Final
                    //if (!currentFieldFromTheFirstClass.isAnnotationPresent(Id.class)) {//Não sobescreve campo id
                        if (nameOfTheFirstField.equals(nameOfTheSecondField)) {
                            currentFieldFromTheFirstClass.setAccessible(true);
                            currentFieldFromTheSecondClass.setAccessible(true);
                            //JSFMessageUtil.addMsgLog(JSFMessageUtil.matricula, ReflectionUtil.class.getCanonicalName(), "ReflectionUtil: " + currentFieldFromTheSecondClass.get(b));
                            currentFieldFromTheFirstClass.set(a, currentFieldFromTheSecondClass.get(b));
                        }
                    //}
                }
            }
        }

        return a;
    }

来电

  public void update(Aluno a) {
        try {
            Aluno aluno = new Aluno();//(Aluno) em.find(Aluno.class, a.getId());
            em.getTransaction().begin();
            //aluno.setNome(a.getNome());
            ReflectionUtil.copyAttributesFromTo(aluno, a);
            em.merge(aluno);
            em.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error " + e.getLocalizedMessage());
            JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        } finally {
            em.close();
        }
    }

真的想不明白你为什么要使用 2 个循环? .. 如果 类 相同。你不需要这样做..只需在1个循环中完成..并使用字段获取保存数据的obj ..并使用set for来设置..这是更好的方法..如果需要相同的对象您可以使用泛型 .. 并且需要相同的对象类型(请求 return 类型)

public static <T> T copyAttributesFromTo(T value, T dataHolder) throws IllegalArgumentException, IllegalAccessException {
        if (value == null || dataHolder == null) {
            throw new IllegalArgumentException();
        }
        final Field[] fields = value.getClass().getDeclaredFields();
        for (Field field : fields) {

            if (!Modifier.isFinal(field.getModifiers())) {               
                field.setAccessible(true);               
                field.set(value, field.get(dataHolder));
            }
        }

        return value; 
    }