You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@isis.apache.org by ch...@wipro.com on 2014/08/08 13:47:23 UTC

InstanceCallbacks.jdoPreStore() method invoked even if JDO persistent(managed) domain object is not modified

DomainObjectContainer.persistIfNotAlready(domainObject) causing invoked of JDO InstanceCallbacks.jdoPreStore() method even if JDO persistent(managed) domain object is not modified.


Code snippet:
isisJdocontainer is of type DomainObjectContainer .
OmScaServiceMap is JDO persistence capable entity.

OmScaServiceMap scaServiceMap = isisJdocontainer.firstMatch(new QueryDefault<OmScaServiceMap>(
                OmScaServiceMap.class, "fetch_OmScaServiceMap_by_sca_service_map", "scaUid", scaUid, "serviceUid",
                serviceUid));
        if (scaServiceMap == null) {
            scaServiceMap = isisJdocontainer.newTransientInstance(OmScaServiceMap.class);
            scaServiceMap.setScaUid(scaUid);
            scaServiceMap.setServiceUid(serviceUid);
            scaServiceMap.setChangeBy(isisJdocontainer.getUser().getName());
            scaServiceMap.setChangeDt(new Date());
        }
        scaServiceMap.setOrgId(orgId);
        isisJdocontainer.persistIfNotAlready(scaServiceMap);

invoked Here I am trying to find OmScaServiceMap by its composite PK. If not exists, create it with newTransientInstance and set the required attribtes while creating a new instance.
Otherwise just set the remaining attributes, that is orgId.

OmScaServiceMap implements InstanceCallbacks and has overridden the method jdoPreStore() which is defined as follows:
public void jdoPreStore() {
        this.setChangeBy(this.container.getUser().getName());
        this.setChangeDt(new Date());
}


Problem:
In case of managed entity(OmScaServiceMap already existing case): even if value of orgId is the same one(as that in the database) that is trying to be set, jdoPreStore is getting called and modifying the changeDt attribute(this.setChangeDt(new Date());) and causing Update query issued to the database.

persistIfNotAlready should not cause invoke of InstanceCallBack.jdoPreStore when domainObject properties are not modified.

Please help on this issue.




The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

Re: InstanceCallbacks.jdoPreStore() method invoked even if JDO persistent(managed) domain object is not modified

Posted by Dan Haywood <da...@haywood-associates.co.uk>.
I think that JDO/DataNucleus is doing the right thing here.  It doesn't
matter to JDO whether the value of orgId being set is the same as the one
already held, the fact is that the setter is called which means that the
object is marked as dirty and therefore JDO will perform a SQL UPDATE and
bump the changeDt attribute for you.

If you don't want this to happen, then I suggest you code up a guard.  I'd
write the method this way:

       OmScaServiceMap scaServiceMap = firstMatch(...);
        if (scaServiceMap == null) {
            scaServiceMap = isisJdocontainer.newTransientInstance(
OmScaServiceMap.class);
            scaServiceMap.setScaUid(scaUid);
            scaServiceMap.setServiceUid(serviceUid);
            scaServiceMap.setChangeBy(isisJdocontainer.getUser().getName());
            scaServiceMap.setChangeDt(clockService.now());
            scaServiceMap.setOrgId(orgId);
            isisJdocontainer.persistIfNotAlready(scaServiceMap);
        } else {
            if(scaServiceMap.getOrgId() != orgId) { // don't touch the
object if already has correct value
                scaServiceMap.setOrgId(orgId);
            }
        }


Also: note in setChangeDt(...) I suggest you get the current time from an
injected clockService, rather than simply calling new Date().  This will
allow you to more easily unit test this code.

HTH
Dan





On 8 August 2014 12:47, <ch...@wipro.com> wrote:

> DomainObjectContainer.persistIfNotAlready(domainObject) causing invoked of
> JDO InstanceCallbacks.jdoPreStore() method even if JDO persistent(managed)
> domain object is not modified.
>
>
> Code snippet:
> isisJdocontainer is of type DomainObjectContainer .
> OmScaServiceMap is JDO persistence capable entity.
>
> OmScaServiceMap scaServiceMap = isisJdocontainer.firstMatch(new
> QueryDefault<OmScaServiceMap>(
>                 OmScaServiceMap.class,
> "fetch_OmScaServiceMap_by_sca_service_map", "scaUid", scaUid, "serviceUid",
>                 serviceUid));
>         if (scaServiceMap == null) {
>             scaServiceMap =
> isisJdocontainer.newTransientInstance(OmScaServiceMap.class);
>             scaServiceMap.setScaUid(scaUid);
>             scaServiceMap.setServiceUid(serviceUid);
>
> scaServiceMap.setChangeBy(isisJdocontainer.getUser().getName());
>             scaServiceMap.setChangeDt(new Date());
>         }
>         scaServiceMap.setOrgId(orgId);
>         isisJdocontainer.persistIfNotAlready(scaServiceMap);
>
> invoked Here I am trying to find OmScaServiceMap by its composite PK. If
> not exists, create it with newTransientInstance and set the required
> attribtes while creating a new instance.
> Otherwise just set the remaining attributes, that is orgId.
>
> OmScaServiceMap implements InstanceCallbacks and has overridden the method
> jdoPreStore() which is defined as follows:
> public void jdoPreStore() {
>         this.setChangeBy(this.container.getUser().getName());
>         this.setChangeDt(new Date());
> }
>
>
> Problem:
> In case of managed entity(OmScaServiceMap already existing case): even if
> value of orgId is the same one(as that in the database) that is trying to
> be set, jdoPreStore is getting called and modifying the changeDt
> attribute(this.setChangeDt(new Date());) and causing Update query issued to
> the database.
>
> persistIfNotAlready should not cause invoke of
> InstanceCallBack.jdoPreStore when domainObject properties are not modified.
>
> Please help on this issue.
>
>
>
>
> The information contained in this electronic message and any attachments
> to this message are intended for the exclusive use of the addressee(s) and
> may contain proprietary, confidential or privileged information. If you are
> not the intended recipient, you should not disseminate, distribute or copy
> this e-mail. Please notify the sender immediately and destroy all copies of
> this message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The recipient
> should check this email and any attachments for the presence of viruses.
> The company accepts no liability for any damage caused by any virus
> transmitted by this email.
>
> www.wipro.com
>