setLoadDelegate works not as described

I try to load the first instance of an collection to the entity editor screen. Therefore i use a loader delegate (see below). This seems to work, until the screen is commited. During commit I get an exception, that a mondatory attribute is not set. If i inspect the SQL statement I recognized, that the entity is not updated with the field values from the screen. It seems that my loaded entity is not really connected to the textfields of the screen. Has anybody an idea what went wrong?

@Install(to = "ownerDl", target = Target.DATA_LOADER)
private Owner ownerDlLoadDelegate(LoadContext<Owner> loadContext) {
    List<Owner> o = dataManager.loadList(loadContext);
    if (o.size() > 0)
        return o.get(0);
    else
        return dataManager.create(Owner.class);
}

Found the solution by myself. The trick is to delete the existing instance from the DataConext before merging a new one to the context. Otherwise the DataContextImpl Instance tries to commit both entities. The instance, that has been internally created has no values and if that is commited, the DB exception (no null values allowed will be thrown). So I changed the code as follow:

@Install(to = "ownerDl", target = Target.DATA_LOADER)
private Owner ownerDlLoadDelegate(LoadContext<Owner> loadContext) {
    dataContext.clear(); // clear existing entity

    // there is only one Owner entity. Load it from database
    List<Owner> o = dataManager.loadList(loadContext);
    Owner owner;
    if (o.size() > 0)
        owner =  o.get(0);  // if owner already exists, show it to the customer
    else
        owner = dataManager.create(Owner.class); // if not create a new one

    return owner;
}
2 Likes