Peter,
 
Thanks for the code.  I will probably make good use of what you sent me.  What I was originially looking for was some way to get the pk using CMP with my enitity.  If BMP is the way to go here, that is what I will do, but I wanted to make sure I'm not missing something the I could do with CMP in the ejbPostCreate. 
 
Thanks again,
 
Bill Pfeiffer
----- Original Message -----
To: jBoss
Sent: Friday, November 03, 2000 9:20 AM
Subject: Re: [jBoss-User] SQLServer getting autoincrement pk

Im using MS SQL server and this is how i do it...
 
The contacts table has an auto inc id field.
 
Alter Procedure CreateNewContact
 (
  @type int,
  @info varchar(50),
  @treadID int
 )
As
Insert into Contacts (Type,Info,ThreadID) VALUES(@type, @info, @treadID)
return @@Identity
 
And my entity bean
 
    public ContactPK ejbCreate( int type, String info, int threadID )  throws CreateException, RemoteException {
        System.out.println( "IN Contact.ejbCreate(int, string, int)" );
 
        this.db_data.type = type;
        this.db_data.info = info;
        this.db_data.threadID = threadID;
        this.db_data.props = new Properties();
 
        Throwable rethrow = null;
        int contactID = 0;
        Connection conn = null;
        CallableStatement ps = null;
        try {
            conn = getDBConnection();
            ps = conn.prepareCall("{?=call CreateNewContact( ?,?,? ) }");
            ps.registerOutParameter( 1, java.sql.Types.INTEGER );
            ps.setInt(2, type);
//            ps.setString(3, info);
            ps.setObject(3, info, java.sql.Types.VARCHAR); // CHECK Bug Id  4274977 at java.sun.com, SetString() padds the db field even when its varchar!!! 
            ps.setInt(4, threadID);
            ps.execute();
 

            this.db_data.id = ps.getInt(1);
            System.out.println("new contact id from db is"  + db_data.id );
 
        } catch ( Exception e ) {
            System.out.println("Error executing sql " + e);
            rethrow = e;
        } finally {
 
            if ( ps != null ) {
                try { ps.close(); } catch(Exception e) {}
                ps = null;
            }
 
            if ( conn != null ) {
                try { conn.close(); } catch(Exception e) {}
                conn = null;
            }
 
            if ( rethrow != null ) {
                throw new CreateException( "Failed to create contact, " + rethrow.toString() );
            }
        }
 
        return new ContactPK( this.db_data.id );
    }
 
I hope this helps.
 
Peter Henderson.
 
----- Original Message -----
To: 'jBoss'
Sent: Friday, November 03, 2000 6:48 AM
Subject: RE: [jBoss-User] SQLServer getting autoincrement pk

should be able to do it with a stored procedure, I'm not sure of the specific syntax to access the last key generated -
-----Original Message-----
From: Bill Pfeiffer [mailto:[EMAIL PROTECTED]]
Sent: Friday, November 03, 2000 5:56 AM
To: jBoss-user
Subject: [jBoss-User] SQLServer getting autoincrement pk

Does anyone know of a strategy for retrieving an  auto-generated pk from sql server autoincrement pk when using entity beans?  Ideally I would like the pk returned from the create to contain the newly generated pk.
 
Any ideas?
 
TIA,
 
Bill Pfeiffer

Reply via email to