cancel
Showing results for 
Search instead for 
Did you mean: 

Autogenerate Primary Key

Former Member
0 Kudos

When creating a new object (or table row) it is convenient to generate a unique identifier using the database auto-increment feature (supported by databases such as MSSQL and MySQL). However, this auto-increment feature is not supported by SAP.

What is the best way to generate a unique identifier (primary key) using Java (eg. for a Entity CMP EJB)?

Can we call the ABAP number range generator from Java ? If so, how can we set this up using the Sneak Preview SAP Enterprise Portal 6.0 ?

Regards,

Alton

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi all,

just refer to section "Automatic Primary Key Generation" in following link

http://help.sap.com/saphelp_nw04/helpdata/en/b3/b238bfd13a8f408958d70a6989d001/content.htm

Benny
Product and Topic Expert
Product and Topic Expert
0 Kudos

Not that complicate:

See methode getUniqueIncreasingNumber() of Interface LogicalLocking at

http://media.sdn.sap.com/html/submitted_docs/60_sp2_javadocs/j2eeengine/index.html

Regards,

Benny

Former Member
0 Kudos

Thank you Benny,

Can you send me implementation notes or code examples ?

Also, is it possible to use getUniqueIncreasingNumber() outside of EJBs; for example, JDO, JDBC or javax.sql.rowset.CachedRowSet ?

Alton

Benny
Product and Topic Expert
Product and Topic Expert
0 Kudos

> Can you send me implementation notes or code examples

> ?

No I can't.

> Also, is it possible to use

> getUniqueIncreasingNumber() outside of EJBs; for

> example, JDO, JDBC or javax.sql.rowset.CachedRowSet

> ?

Sure. This is actually part of the Locking API that is available with WebAS.

You seem to be new to java? You only have to import the Interface to your code and have the lib in your path (project properties).

If this is new to you I would advice to learn java a little deeper before continuing. Some things are really very different from ABAP. And java tutorials are legion.

Regards,

Benny

Former Member
0 Kudos

Benny,

Thanks for your lead. I got it working on "SAP Enterprise Portal 6.0 SP4 NetWeaver Developer Sneak Preview" as follows:

1) Added the following External JARs to the Java Build Path

\usr\sap\P66\JC00\j2ee\cluster\server0\bin\services\applocking\applocking.jar

\usr\sap\P66\JC00\j2ee\cluster\server0\bin\system\exception.jar

\usr\sap\P66\JC00\j2ee\cluster\server0\bin\system\frame.jar

2) Use the following code:

Context initialContext = new InitialContext();

LogicalLockingFactory lockingFactory = (LogicalLockingFactory) initialContext.lookup(LogicalLockingFactory.JNDI_NAME);

LogicalLocking locking = lockingFactory.createLogicalLocking("ObjectNameSpace","Description");

long id = locking.getUniqueIncreasingNumber();

Former Member
0 Kudos

Hi Folks,

thanks for this great post

(I used "applocking"-service now from an portal-component (after setting the right references))

but: (just to be sure) the "increasing number" increases more or less arbitrarily, right? Not like "1, 2, 3" - but just a big number increasing somehow, right?

Or am I doing something wrong? Do I have to cast the long somehow or something else?

Thanks,

Simon

matt_steiner
Active Contributor
0 Kudos

Hi folks,

just for your information. If you are looking for a way to generate a Globally Unique Identifier (GUID) there's also the GUIDService on the WebAS.


import com.sap.guid.GUIDGeneratorFactory;
import com.sap.guid.IGUIDGenerator;

...


IGUIDGenerator generator = GUIDGeneratorFactory.getInstance().createGUIDGenerator();
String guid = generator.createGUID().toHexString();

Just to let you know

Matthias

Former Member
0 Kudos

Thanks Matthias!

BTW, the jar can be found here: usr\sap\<YOURSID>\<YOURINSTANCE>\j2ee\cluster\server0\bin\ext\com.sap.guid\guidgenerator.jar

Regards,

Kristian

Former Member
0 Kudos

Hi Simon,

I get a ClassCastException when I try to get the LogicalLockingFactory from JNDI registry. To which Class do you cast the result from the lookup?

Direct casting to LogicalLockingFactory doesn`t seem to work here...

Thanks & Regards,

Kristian

Former Member
0 Kudos

Hi Kristian,

yes, direct castin.


LogicalLockingFactory lfact =
					(LogicalLockingFactory) new InitialContext().lookup(
						LogicalLockingFactory.JNDI_NAME);

Maybe you fetched the wrong Object from JNDI?

Regars,

Simon

P.S:

Sorry for late answer - I didnt see your posting..