EJB programming & troubleshooting: how to generate the unique primary key for entity bean
- Posted by: u u
- Posted on: August 12 2003 07:34 EDT
i am the beginner to entity bean,and usually use the toplink as the o-r mapping tool.it can generate the unique primary key automatically for me,i don't need to worry about it.
now i transfer to entity bean,i found that i must generate the pk in my code(if i use the integer id as primary key),then invoke the "create" method to insert a record to database,but how can i keep the id unique?
who can introduce me the best mechanism to generate pk for the entity bean?
thank you very much!
- how to generate the unique primary key for entity bean by Medan Andrei on August 12 2003 08:39 EDT
- how to generate the unique primary key for entity bean by web guy on August 12 2003 09:46 EDT
- The other way is by Raheel haider on August 12 2003 13:33 EDT
- Thread by Ian Mitchell on August 12 2003 15:14 EDT
for autogenerate the primary key for an entity bean, I write a method into a session bean, like that public Long giveId(String sequenceName, String databaseDatasource) ). This body of this method is
con = this.getConnection( databaseDatasource );
stmt = con.createStatement();
res = stmt.executeQuery("SELECT " + sequenceName + ".nextval from DUAL");
while( res.next() )
id = res.getLong(1);
con.close(); As you can see i call the query SELECT nameSequence.nextval FROM DUAL. An the method getConnection is a private method who will return a connection from the datasource with name databaseDatasource. The body of that method is as follow:
Connection con = null;
Throwable th = null;
Hashtable h = new Hashtable();
InitialContext ctx = new InitialContext(h);
DataSource ds = (DataSource)ctx.lookup( databaseDatasource );
con = ds.getConnection();
h = null; ctx= null; ds = null;
catch( Exception ex)
th = ex;
if (th != null)
throw new IdException(th);
When you will call the method giveId the sequencename you write there is the name for the sequence you want to increase the value and the databaseDatasource you have to write there is the name of the datasource you use to configure your application server
You don't mention which J2ee server you are using.
Jboss can automatically create a ID for you using several different methods including using a class you specify. Look for <entity>
http://www.mail-archive.com/jboss-user at lists dot sourceforge dot net/msg29049.html
WebLogic 6.1 can use something like
Look here: http://rickhightower.blogspot.com/2002_09_01_rickhightower_archive.html
Hi, Jboss gives a functionality to create auto Primary key, but what if you need to create the primary key with some customize formulla..
you can do this within the session bean or define a method in entity bean home interface if you are only dealing with entity bean.
you dont' need to call create first, you can simply get the refference of remote interface by calling let say this sample code present..
when dealing with only entity bean no session bean
int id or String id=home.getNextPrimaryKey();
remeber "getNextPrimaryKey() is the method which is defined in you entity home interface reason for defining it into the home interface rather then remote interface is that this method is not specifc to any given data (row) the benefit of defining this method inside the entity home interface is that they are called from a bean in the pool before the bean is associated with any given data.
if you are also using session bean in your application then do the same thing from session bean, because it save the extra remote call.
This thread on primary key generation appeared about three years ago, and may still be of interest:
thanks for all of your helps!