Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

entity with auto-generated primary key  RSS feed

 
Raj Pod
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using Weblogic 6.0 and EJB 2.0 specification.
I am generating an entity bean. The table for this entity bean has an auto-generated primary key(you may find this in MS-Access). Now how do I tell the entity bean that the primary key is automatically generated and not to be provided by the client?
I tried by not providing the primary key in the create method, but it throws an error stating that the primary key has to be set.
Please provide an answer to this solution.
Raj
 
jose barros
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, excuse-me for my poor English, but I�m trying to improve this!
I�m solved this problem in my EMP beans appliyng these methods:
//pk is the primary key field.
//field_1 and field_2 are other database fields.
//table is the database�s name.
public String ejbCreate(String field_1, String field_2 ...) throws CreateException { //Don�t pass the Primary Key.
boolean exists = false;
try {
_insert(field_1, field_2);
pk = _getPrimaryKey();
}
catch(Exception e) {
e.printStackTrace();
}
return pk;
}
private void _insert(String field_1, String field_2) throws SQLException {
String insertStatement = "insert into table (field_1, field_2) values (?, ?)";
PrepareStatement pstm = con.PrepareStatement(insertStatement);
pstm.setString(1, field_1);
pstm.setString(2, field_2);
pstm.executeUpdate();
pstm.close();
}
private String _getPrimaryKey() throws SQLException {
String pk = "";
String selectStatement = "select max(primary_key_field) from table";
ResultSet rs = db.executeQuery(selectStatement);
if (rs.next()) {
pk = rs.getString(1);
return pk;
} else {
throw new EJBException("Unable to find a primary key!");
}
}

With me it�s work fine, did you understood? The basic logic is a method what find the last primary before de insert statement.
SPARC
 
Raj Pod
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
Thanks for the response, Mr. Jose. And there is nothing wrong with your english.
But I have forgot to mention that I am looking for the solution in CMP entity bean and not BMP. You have provided me a solution for BMP entity bean.
What do I do for a CMP bean? I have tried to avoid setting the id for the bean in the ejbCreate() method, but the compiler throws the error that I have to explicitly set the id.
Please help.
Rajiv

Originally posted by jose barros:
Hi, excuse-me for my poor English, but I�m trying to improve this!
I�m solved this problem in my EMP beans appliyng these methods:
//pk is the primary key field.
//field_1 and field_2 are other database fields.
//table is the database�s name.
public String ejbCreate(String field_1, String field_2 ...) throws CreateException { //Don�t pass the Primary Key.
boolean exists = false;
try {
_insert(field_1, field_2);
pk = _getPrimaryKey();
}
catch(Exception e) {
e.printStackTrace();
}
return pk;
}
private void _insert(String field_1, String field_2) throws SQLException {
String insertStatement = "insert into table (field_1, field_2) values (?, ?)";
PrepareStatement pstm = con.PrepareStatement(insertStatement);
pstm.setString(1, field_1);
pstm.setString(2, field_2);
pstm.executeUpdate();
pstm.close();
}
private String _getPrimaryKey() throws SQLException {
String pk = "";
String selectStatement = "select max(primary_key_field) from table";
ResultSet rs = db.executeQuery(selectStatement);
if (rs.next()) {
pk = rs.getString(1);
return pk;
} else {
throw new EJBException("Unable to find a primary key!");
}
}

With me it�s work fine, did you understood? The basic logic is a method what find the last primary before de insert statement.
SPARC

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!