Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to access Primary Key instance of entity relationship?
How to access Primary Key instance of entity relationship? [message #1064468] Wed, 19 June 2013 12:23 Go to next message
Markus KARG is currently offline Markus KARGFriend
Messages: 28
Registered: June 2011
Junior Member
I want to turn entity instances into XML representations using JAXB. For LAZY references, I want to simply contain a URL (made up of the referenced Primary key), so that each XML receiver can decide on his own whether to explicitly load this extra information or not. So I wrote an XmlAdapter to turn relationships into URLs, which works well, but has the drawback, that as soon as I access the LAZY relationship to find out the primary key, certainly it is loaded by the entity manager. This is not nice, as I do not need all this data -- but solely the primary key.

So I wonder whether there is a way to get the primary key of a referenced entity without actually loading its complete state into memory?
Re: How to access Primary Key instance of entity relationship? [message #1065390 is a reply to message #1064468] Tue, 25 June 2013 14:26 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Please log a bug to have a helper API added for this.

If you also map the foreign key as a @Basic (you can mark it insertable/updateable=false), then you can access the id.

You could also use,

AbstractSession session = (AbstractSession)em.unwrap(Session.class);
OneToOneMapping mapping = session .getDescriptor(entity.getClass()).getObjectBuilder().getMappingForAttributeName(attribute);
Object id = mapping.valueFromObject(entity, mapping.getForeignKeyFields().get(0), session);



James : Wiki : Book : Blog : Twitter
Re: How to access Primary Key instance of entity relationship? [message #1065392 is a reply to message #1065390] Tue, 25 June 2013 14:27 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Another option is to encode the root object id and the attribute name. Then you just load the root object, and access the attribute when the url is accessed. This will also work for collection relationships, so is probably better.


James : Wiki : Book : Blog : Twitter
Previous Topic:Validation on preUpdate checks unchanged entities
Next Topic:entitymanager problem merge
Goto Forum:
  


Current Time: Mon Dec 22 07:50:32 GMT 2014

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

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