+ e.getMessage());
}
finally{
if (rs != null) try { rs.close(); } catch (Exception e){}
rs = null;
if (statement != null) try { statement.close(); } catch (Exception
e){}
statement = null;
if (conn != null) try { conn.close(); } catch (Exception e){}
conn = null;
}
}
public String getRefQuery(String ref) throws NoSuchObjectException{
CCHInitialContext context = getInitialContext();
String query = (String) context.lookup(
"java:comp/env/IDQuery/" + ref, String.class );
return query;
}
public Connection getConnection(String ref)
throws NoSuchObjectException, SQLException {
CCHInitialContext context = getInitialContext();
String dbref = (String)context.lookup(
"java:comp/env/db/" + ref, String.class );
DataSource ds = (DataSource)context.lookup(
"java:comp/env/" + dbref, DataSource.class);
Connection conn = ds.getConnection();
return conn;
}
}
=Class that uses id service
public class ShareTypeEntity extends AbstractEntityBean{
public Long ID;
public String description;
private transient IdentityServiceHome idServiceHome;
public Long getID() throws RemoteException{
return ID;
}
public void setID(Long newID){
ID = newID;
}
public String getDescription() throws RemoteException{
return description;
}
public void setDescription(String newDesc) throws RemoteException{
description = newDesc;
}
/**
* Dont Allow removes
*/
public void ejbRemove() throws RemoveException, EJBException,
java.rmi.RemoteException{
throw new RemoveException("Remove not supported");
}
public Long ejbCreate(String desc) throws RemoteException,
CreateException{
if ( desc == null ) throw new CreateException("Description Required");
if ( desc.trim().length() == 0 ) throw new CreateException("Description
Required");
System.err.println( "ShareType create called desc=" + desc);
description = desc;
ID = getNextId();
return null;
}
public void ejbPostCreate(String desc) throws RemoteException,
CreateException{
}
private Long getNextId() throws CreateException, EJBException,
RemoteException{
IdentityService idService = getIdentityServiceHome().create();
return idService.nextVal("ShareType");
}
private IdentityServiceHome getIdentityServiceHome() throws
RemoteException{
if ( idServiceHome == null) {
idServiceHome = (IdentityServiceHome)super.getHome(
"java:comp/env/ejb/IdentityService",IdentityServiceHome.class);
}
return idServiceHome;
}
}
Hope this helps, As you can see with the SQL descriptor I pass 2
statements to the database
to get the Updated value. As it is in its own transaction this should be
fine, providing the database can handle 2 statements.
Noel
- Original Message -
From: "Mike Cannon-Brookes" <[EMAIL PROTECTED]>
To: "Orion-Interest" <[EMAIL PROTECTED]>
Sent: Saturday, April 08, 2000 7:40 AM
Subject: RE: CMP and Identity columns
> Shayne,
>
> As far as I know you cannot use identity (or autonumber fields) with EJBs.
> You have to create the primary key within Java.
>
> Anyone else?
>
> Mike
>
> > -Original Message-
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED]]On Behalf Of Shayne Hughes
> > Sent: Saturday, 8 April 2000 9:38
> > To: Orion-Interest
> > Subject: CMP and Identity columns
> >
> >
> > I have a table on Microsoft SQL Server that has an identity column such
> > as...
> >
> > CREATE TABLE MyTable (
> > id int IDENTITY (1, 1) PRIMARY KEY NOT NULL ,
> > Name varchar (255) NULL
> > )
> >
> > I'd like to create a CMP EJB for this table. Is it possible to instruct
> > Orion to not specify a value for the "id" field when inserting a
> > new row and
> > to execute the proper SQL command to get back the "id" of the new row?
> >
> > My classes only have create methods that specify "Name". Here's
> > the message
> > from the exception that occurs...
> >
> > Error creating EntityBean: [ECOLI]Cannot insert explicit value
> > for identity
> > column in table 'GelGroup' when IDENTITY_INSERT is set to OFF.
> >
> > I hope this is on topic. My reseach points to this being a
> > container config
> > thing.
> >
> > Shayne
> >
> >
> >
> >
>
>