cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cghersi <cristiano.ghe...@abodata.com>
Subject RE: Temporary ID hasn't been replaced on commit
Date Fri, 09 Mar 2012 14:10:57 GMT
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 

  _____  

If you reply to this email, your message will be added to the discussion
below:

http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit
-tp3801043p3806195.html 

To unsubscribe from Temporary ID hasn't been replaced on commit, click here
<http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe
_by_code&node=3801043&code=Y3Jpc3RpYW5vLmdoZXJzaUBhYm9kYXRhLmNvbXwzODAxMDQzf
DUzMzUzMjY3> .
 
<http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewe
r&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNam
espace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.Nod
eNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emai
ls%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> NAML 



--
View this message in context: http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3812749.html
Sent from the Cayenne - User mailing list archive at Nabble.com.
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message