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;
}
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.
source
share