I'd be interested to hear what others think of this method of using MSSQL's
autoincrement type as PK.  I have tried this and it seems to work in simple
tests.  Yeah it's not portable and probably breaks several rules.

For this table...
CREATE TABLE [dbo].[Lab] (
 [LabID] [int] IDENTITY (1, 1) NOT NULL ,
 [Name] [varchar] (255) NOT NULL ,
 [LastUpdate] [datetime] NOT NULL
)

I have an CMP EJB with the following in the orion-ejb-jar.xml...
   <finder-method query="insert into Lab (Name,LastUpdate) values
($1,GetDate()); select LabID,Name,LastUpdate from Lab where
LabID=@@IDENTITY" partial="false">
    <method>
     <ejb-name>Lab</ejb-name>
     <method-name>findNew</method-name>
     <method-params>
      <method-param>java.lang.String</method-param>
     </method-params>
    </method>
   </finder-method>

So, you never create a new bean in the normal CMP way, but if you need a new
one, you call this finder which creates a new one and finds it.

Shayne


----- Original Message -----
From: "Sven van 't Veer" <[EMAIL PROTECTED]>
To: "Orion-Interest" <[EMAIL PROTECTED]>
Sent: Tuesday, November 14, 2000 10:21 AM
Subject: Re: SQL Server - Autoincrement Primary Keys


> Chris Miller wrote:
> >
> > Hi all,
> >
> > This is possibly more of a generic EJB question, although maybe not
since
> > Orion has to implement the persistence...
> >
> > I was wondering if it is possible to use CMP beans against an existing
> > SQL-Server database that has autoincrement primary keys. It's not
possible
> > to insert a value for the key (have to supply null), so using something
like
> > counter.jar to override the PK generation is not an option. Has anyone
done
> > this successfully? What's the trick, or am I stuck with BMP (as I
suspect is
> > the case)?
> >
> Using auto incrementing fields in EJB is not very usefull since they may
> map to databases not supporting such features, it's just like stored
> procedures. They might seem to look like a good idea, but it just makes
> the application much less portable.
>
> Orion includes an EJB which can generate a unique id for you, it's in
> counter.jar (I think you can find it in the news app).
>
> However if you really have to use an existing DB, you're either stuck
> with using BMP or you could write an entitybean (CMP) which only does
> the updates for you and may delete te bean by calling remove on the bean
> and a stateles session bean that handles the insertions for you.
>
> sven
>
> --
>
============================================================================
==========
> Sven E. van 't Veer
> http://www.cachoeiro.net
> Java Developer             [EMAIL PROTECTED]
>
============================================================================
==========
>


Reply via email to