Refresh JPA object with reflection not working?

I have an object that looks something like this: (I am coding on a web page, so I apologize for any errors)

@Entity
public class Entity {
    @Id
    private Long id;
    private String field;

    // Insert getters and setters here...
}

I am trying to manipulate it with reflection:

Long id = 1;

Entity entity = myDao.getEntity(id);

entity.setField("set directly");

Field[] fields = entity.getClass().getDeclaredFields();

for (Field f : fields) {
    if (f.getName().equals("field")) {
        f.setAccessible(true);
        f.set(entity, "set using reflection");
        f.setAccessible(false);
    }
}

System.out.println(entity.getField());

This program prints "set using reflection". However, in the database, the value specified using reflection is not updated:

SELECT * FROM ENTITY WHERE ID = 1
ID     FIELD
1      set directly

This is strange. I could swear it worked, but now it is not. Can't you manipulate objects using reflection?

I use EclipseLink 1.1.1 if that matters.

+3
source share
3 answers

. , , , API- Persistence .

, , API- persistence API .

.

+5

, , persistence, (, , ). . , () , . , , , ?

+1

, , , . EclipseLink, , , myDao.getEntity(id); , EclipseLink.

0

Source: https://habr.com/ru/post/1712004/


All Articles