[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.rt.eclipselink] Updating an entity using reflection does not work?
|
- From: Pasi.Kovanen@xxxxxx (Pasi Kovanen)
- Date: Tue, 7 Jul 2009 13:31:43 +0000 (UTC)
- Newsgroups: eclipse.rt.eclipselink
- Organization: Eclipse
- User-agent: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
I have an entity which looks something like this:
@Entity
public class Entity {
@Id
private Long id;
private String field;
// Insert getters and setters here...
}
I try to manipulate it using reflection:
Long id = Long.valueOf(1);
Entity entity = em.find(Entity.class, 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 set using reflection does not get updated:
SELECT * FROM ENTITY WHERE ID = 1
ID FIELD
1 set directly
Is it really so that you cannot manipulate entities using reflection? Is
there a way to overcome this?
I'm using EclipseLink 1.1.1.