TopLink Wiki

Main
About
Javadoc
Forum
Resources

RecentChanges
About Wiki
Find Pages
Page Index

Set your name in
UserPreferences

Edit this page


Referenced by
TopLinkGotchas
UnitOfWork
Main
ConformResultsInUnit...




JSPWiki v2.0.52


FindingNewObjects


A common TopLink problem is creating a new object through the UOW, and wanting to access it later (before commit). Because the object is new, it doesn't have a primary key yet, and one is therefore unable to look it up.

Person personClone = (Person) uow.newInstance(Person.class);
personClone.setId(1);
personClone.setName("me");
 
// uow.commit();  --- not committed
 
ExpressionBuilder builder = new ExpressionBuilder();
Expression expr = builder.get("name").equal("me");
Person personFound = (Person) session.readObject(Person.class, expr);
// personFound is null 

Possible solutions:

  1. assign the object a primary key right away, with UOW#assignSequenceNumber
  2. use a conforming query that will search the UOW, see ConformResultsInUnitOfWork
  3. pass the new object around using a property:

session.setProperty("NEW PERSON", personClone);
// later 
Person newPerson = (Person) session.getProperty("NEW PERSON");

See Also

forum:280729, forum:240825

The UnitOfWorkPrimer discusses this trick also.


Edit this page   More info...   Attach file...
This page last changed on 25-May-2005 14:19:30 PDT by 64.168.145.189.