Skip to main content

using JPA, can I get last inserted PK

No replies
rrlangly
Offline
Joined: 2012-06-25
Points: 0

I've been using JPA to instert entities into a database but I've run up against a problem where I need to do an insert and get the primary key of the record last inserted.

Using PostgreSQL I would use an INSERT RETURNING statement which would return the record id, but with an entity manager doing all this, the only way I know is to use SELECT CURRVAL.

So the problem becomes, I have several data sources sending data into a message driven bean (usually 10-100 messages at once from each source) via OpenMQ and inside this MDB I persists this to PostgreSQL via the entity manager. It's at this point *I think* there will be a "race condition like" effect of having so many inserts that I won't necessarily get the last record id using SELECT CURRVAL.

My MDB persists 3 entity beans via an entity manager like below.

Any help on how to better do this much appreciated.

<br />
   public void onMessage(Message msg) {<br />
        Integer agPK = 0;<br />
        Integer scanPK = 0;<br />
        Integer lookPK = 0;</p>
<p>        Iterator iter = null;<br />
        List<Ag> agKeys = null;<br />
        List<Scan> scanKeys = null;</p>
<p>        try {<br />
            iag = (IAgBean) (new InitialContext()).lookup(<br />
                    "java:comp/env/ejb/AgBean");</p>
<p>            TextMessage tmsg = (TextMessage) msg;</p>
<p>			// insert this into table only if doesn't exists<br />
            Ag ag = new Ag(msg.getStringProperty("name"));</p>
<p>            agKeys = (List) (iag.getPKs(ag));</p>
<p>            iter = agKeys.iterator();</p>
<p>            if (iter.hasNext()) {<br />
                agPK = ((Ag) iter.next()).getId();<br />
            }<br />
            else {<br />
				// no PK found so not in dbase, insert new<br />
                iag.addAg(ag);<br />
                agKeys = (List) (iag.getPKs(ag));<br />
                iter = agKeys.iterator();</p>
<p>                if (iter.hasNext()) {<br />
                    agPK = ((Ag) iter.next()).getId();<br />
                }<br />
            }</p>
<p>			// insert this into table always<br />
            iscan = (IScanBean) (new InitialContext()).lookup(<br />
                    "java:comp/env/ejb/ScanBean");</p>
<p>            Scan scan = new Scan();<br />
            scan.setName(msg.getStringProperty("name"));<br />
            scan.setCode(msg.getIntProperty("code"));</p>
<p>            iscan.addScan(scan);</p>
<p>            scanKeys = (List) iscan.getPKs(scan);</p>
<p>            iter = scanKeys.iterator();</p>
<p>            if (iter.hasNext()) {<br />
                scanPK = ((Scan) iter.next()).getId();<br />
            }</p>
<p>			// insert into this table the two primary keys above</p>
<p>            ilook = (ILookBean) (new InitialContext()).lookup(<br />
                    "java:comp/env/ejb/LookBean");</p>
<p>            Look look = new Look();</p>
<p>            if (agPK.intValue() != 0 && scanPK.intValue() != 0) {<br />
                look.setAgId(agPK);<br />
                look.setScanId(scanPK);</p>
<p>                ilook.addLook(look);<br />
            }<br />
	// ...<br />