Thanks MRG. It is clear now.
I am now getting another problem now. Below is the code [code part 1] based
upon your inputs. However after initiating an object from the context -
there is no ID in the objects [code part 2]. When I debug all the id's are
temp ids and "*lf.getLogicalFolderId()*" in code part 2 is null - where as
it should not be null - if onPostAdd worked fine.
Code part 1
"
import blackcomb.postgresdb.auto._LogicalFolder;
import java.util.UUID;
import org.apache.cayenne.PersistenceState;
public class LogicalFolder extends _LogicalFolder {
public UUID getLogicalFolderUUID()
{
return UUID.fromString(getLogicalFolderId());
}
public void setLogicalFolderUUID(UUID uuid)
{
setLogicalFolderId(uuid.toString());
}
@Override
protected void onPostAdd()
{
setLogicalFolderId(UUID.randomUUID().toString());
}
}
"
Code Part 2
"
ObjectContext oc = blgetcontext.getObjectContext();
//Create UserMaster
UserMaster um = oc.newObject(UserMaster.class);
um.setUserName("sysadm");
um.setPassword("sysadm");
//Create CreationLog - Common
CreationLog clc = oc.newObject(CreationLog.class);
//Set CreationLog relation with User master
clc.setCLUserMaster(um);
//Create UserDomain for SysAdmin
UserDomain ud = oc.newObject(UserDomain.class);
ud.setCreatedOn(new Date());
ud.setName("sysadmdom");
ud.setActive(Boolean.TRUE);
ud.setIsDefault(Boolean.FALSE);
//Set Userdomain Relation with UserMaster
ud.setUDUserMaster(um);
//Create LogicalFolder for Sys Admin - User
LogicalFolder lf = oc.newObject(LogicalFolder.class);
lf.setMaxSize(10000);
lf.setLFCreationLog(clc);
lf.setContainerId(UUID.randomUUID().toString());
lf.setLFUserDomain(ud);
lf.setOwnerRootFolderId(lf.getLogicalFolderId().toString());
"
On Sat, Jul 28, 2012 at 2:29 AM, Michael Gentry <[email protected]>wrote:
> Hi Kanwar,
>
> I answered this in another thread, but here is a better explanation
> for you. You don't need to check for null in onPostAdd() and you
> especially don't need to check if getObjectId() is null (it won't be,
> since it is in a DataContext). If you did need to check for null for
> some reason, perhaps check if getLogicalFolderId() returns null. All
> new objects registered in a DataContext have a temporary objectID
> (hence it will not be null) and all persisted objects have a permanent
> objectID (which also will not be null), therefore none of your Cayenne
> objects should ever have a null objectID if residing in a DataContext.
>
> mrg
>
> On Fri, Jul 27, 2012 at 7:14 AM, Kanwar Manish <[email protected]>
> wrote:
> > I am using Cayenne 3.1B1.
> >
> > Overriding onPostAdd is not working. I have added the callback in the
> > modeller added the code below
> >
> > @Override
> > protected void onPostAdd()
> > {
> > if (getObjectId() == null)
> > {
> > setLogicalFolderId(UUID.
> > randomUUID().toString());
> > }
> > }
> >
> > Basically - trying to initialize the Primary Key - but this is not
> working.
> > When adding multiple related objects - id's of the objects obtained
> through
> > - context.newObject() are not having any id.
> >
> > Can you point me - what am I doing wrong/missing here?
> >
> > Thanks
> > Kanwar Manish
>