cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Temporary ID hasn't been replaced on commit
Date Fri, 09 Mar 2012 14:39:39 GMT
> Is there any effective way to directly retrieve the ID of the just added
> object after the context.commitChanges() invocation?

After commit the ID should be available via normal Cayenne APIs (e.g. Cayenne.inPkForObject).
So there's something else at play too.

Andrus

On Mar 9, 2012, at 9:10 AM, cghersi wrote:
> Hi all, perhaps I’ve understood something about the issue:
> 
> Seems that the problem arises when I add a new object to the DB: just after
> the insert, I need the autogenerated PK, and this seems to put in troubles
> the method DataObjectUtils.intPKForObject(Persistent);
> 
> Which sometimes throws the Exception in the subject.
> 
> 
> 
> Is there any effective way to directly retrieve the ID of the just added
> object after the context.commitChanges() invocation?
> 
> 
> 
> Thank you very much for your help.
> 
> 
> 
> Best
> 
> cghersi
> 
> 
> 
> From: cghersi [via Cayenne] [mailto:ml-node+s195n3806195h77@n3.nabble.com] 
> Sent: mercoledì 7 marzo 2012 09:43
> To: cghersi
> Subject: Re: Temporary ID hasn't been replaced on commit
> 
> 
> 
> Hi Andrus, 
> 
> here there's the code that I use to save my object: 
> 
>        private AtomicReference<DataContext> context = new
> AtomicReference<DataContext>(config.getDomain("PacketDomain").createDataCont
> ext()); 
> 
>        public Network GetNetworkByPK(Integer pkID) { 
>                ObjectId id = new ObjectId(Network.class.getSimpleName(),
> Network.ID_PK_COLUMN, pkID); 
> 
>                // this constructor implicitly uses "CACHE_REFRESH" policy,
> so a fresh object will be returned 
>                ObjectIdQuery query = new ObjectIdQuery(id); 
>                return
> (Network)DataObjectUtils.objectForQuery(context.get(), query); 
>        } 
> 
>        public PhysicalNode GetPhysicalNodeByMac(byte[] macAddr){ 
>                Expression qualifier =
> ExpressionFactory.matchExp(PhysicalNode.MAC_ADDRESS_PROPERTY, macAddr); 
>                List<PhysicalNode> list = query(qualifier,
> PhysicalNode.class); 
> 
>                //there can be only a single result from this query: 
>                return list.get(0); 
>        } 
> 
>        public synchronized PhysicalNode AddPhysicalNode(byte[] macAddr,
> byte[] nsap, int netId, short type, 
>                        boolean isOnline, String defaultLabel){ 
> 
>                PhysicalNode pn=null; 
>                Network n = GetNetworkByPK(netId); 
>                if(n!=null){ 
>                        pn = GetPhysicalNodeByMac(macAddr); 
>                        if(pn==null){ 
>                                pn = createNewObj(PhysicalNode.class); 
>                                pn.setMacAddress(macAddr); 
>                                pn.setLabel(defaultLabel); 
>                                pn.setCoordX((double)-1); 
>                                pn.setCoordY((double)-1); 
>                                pn.setCoordZ((double)-1); 
>                                pn.setCreation((new Date()).getTime()); 
>                        } 
>                        pn.setNetwork(n); 
>                        pn.setNsap(nsap); 
>                        pn.setType(type); 
>                        pn.setIsOnline(isOnline); 
>                        CommitObjects(PhysicalNode.class); 
>                } 
>                else{ 
>                        throw new ValidationException("Network not in DB"); 
>                } 
>                return pn; 
>        } 
> 
> The problem is not systematic, but may occur with randomness if I force the
> DB to add a great number of PhysicalNode objects in a little time window. 
> 
> Please, let me know if you have any idea! 
> 
> Thank you very much 
> Best 
> cghersi 
> 
>  _____  


Mime
View raw message