Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Updating an entity using reflection does not work?
Updating an entity using reflection does not work? [message #389795] Tue, 07 July 2009 13:31 Go to next message
Torben Putkonen is currently offline Torben PutkonenFriend
Messages: 34
Registered: July 2009
Member
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.
Re: Updating an entity using reflection does not work? [message #390025 is a reply to message #389795] Wed, 08 July 2009 13:37 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

By default when using weaving/agent EclipseLink uses attribute change
tracking to detect changes. This will not detect changes made through
reflective field access (method access is ok though).

You can change the default using the @ChangeTracking annotation to
deferred which will detect change made through reflection.
i.e.
@ChangeTracking(ChangeTrackingType.DEFERRED)

http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/annotations/ChangeTracking.html

You could also disable weaving, or weaving of change tracking in the
persistence.xml using,

"eclipselink.weaving.changetracking"="false"

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Previous Topic:Open source projects using eclipselink
Next Topic:JPA and JSP
Goto Forum:
  


Current Time: Fri Mar 29 09:36:36 GMT 2024

Powered by FUDForum. Page generated in 0.02960 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top