Hi,
None of the other replies I've seen have pointed out that your max() + 1
method for obtaining the next id is unsuitable for a multi user
environment, unless you want to lick the entire table for the duration of
the create method. There are zillions of posts on this topic.
One solution, since you are apparently using bmp and a db with
autoincrementing column types, is to have a separate "company_id" table
whos only column is autoincrement (idnetity???). In your create method,
query this table for a new value BEFORE you insert your new row, and use
the returned number. You might need a sp in sql server to get the id back
in one call.
On 2001.06.07 04:56:19 -0400 Chris Tragas wrote:
> Hi.
>
> I've been on this problem for days now and I'm crying out for some help.
>
> What I am trying to achive is to have a cmp bean that is mapped to a
> database table in which the primary key is also an identity column (ie
> self
> incrementing) I've read a lot of the queries and answers that have been
> sent
> but I havent found a solution that suffices.
>
> Versions i'm using are: JBoss 2.2.2 with Tomcat 3.2.1 and SQL Server 7 on
> an
> NT4 server.
>
> What I'm trying to achieve is when i create a new CustomerDetails Bean
> via
> the Home.create method I want the ejbCreate method to look up the
> database
> to find the max(company_id) and assign the primary key value of the new
> CustomerDetails Bean i'm trying to create to max(company_id) + 1;
>
> I successfully deploy the bean (sometimes, I wont even begin to explain
> the
> inconsistencies I've been experiencing with Jboss deployment - for
> instance.
> it spits out error messages regarding method types that don't even exist-
> is
> there a caching issue here???);
> Then I have a jsp client that access the cmp bean CustomerDetails. When I
> reference home.create() method the container management tries to insert
> into
> the database a row with the column value of the primary key as null;
> I assumed that when calling the home.create() method, the corresponding
> ejbCreate method in the remote bean CustomerDetailsEJB would be called;
> but
> it doesnt appear to be getting there and I have no way of telling;
>
> HELP? Please? Anyone? Things are growing out of my forehead that shouldnt
> be
> as a result of this "hic-up";
>
> Thanks in advance.
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> My table definition is:
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
> CREATE TABLE COMPANY_DETAILS (
> COMPANY_ID numeric (18, 0) NOT NULL , // this
> column
> is the identify (self incrementing) column
> COMPANY_NAME varchar (255) NULL ,
> COMPANY_ABN varchar (20) NULL ,
> COMPANY_DESC varchar (255) NULL ,
> COMPANY_LOGO_IMG varchar (255) NULL
> )
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> My Home class is as follows:
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
> package com.atomicmedia.totalpromoter.dao.companydetails;
>
> import java.rmi.RemoteException;
>
> import javax.ejb.*;
>
> public interface CompanyDetailsHome extends EJBHome {
>
> /**
> * create a new CompnayDetails Record
> public CompanyDetails create(String id)
> throws RemoteException, FinderException;
> */
>
> /**
> * Find a CompnayDetails Record by the primary key
> */
> public CompanyDetails findByPrimaryKey(Integer aKey)
> throws RemoteException, FinderException;
>
> /**
> * create an ejb
> */
> public CompanyDetails create()
> throws RemoteException, CreateException;
> }
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> My Entity Bean is as follows
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
> package com.atomicmedia.totalpromoter.dao.companydetails;
>
> import javax.ejb.*;
> import javax.naming.*;
> import java.sql.*;
>
> public class CompanyDetailsEJB implements EntityBean {
> EntityContext ejbContext;
>
> public Integer companyId;
>
> /* Container managed fields */
> public String companyName;
> public String companyAbn;
> public String companyDesc;
> public String companyLogoImg;
>
> public CompanyDetailsEJB() {
> }
>
> public void setEntityContext(EntityContext context){
> ejbContext = context;
> }
>
> public void unsetEntityContext(){
> ejbContext = null;
> }
>
> private Connection getConnection() throws SQLException,
> javax.naming.NamingException {
> InitialContext jndiContext = new InitialContext();
> javax.sql.DataSource source = (javax.sql.DataSource)
> jndiContext.lookup("java:TotalPromoterPool");
> return source.getConnection();
> }
>
> public Integer ejbCreate() {
>
> Integer cId = null;
> Connection con = null;
>
> try {
> con = this.getConnection();
> PreparedStatement sqlStmt =
> con.prepareStatement("INSERT INTO company_details " +
>
> "(company_name " +
> ",company_abn " +
> ",company_desc "
> +
>
> ",company_logo_img) " +
> "VALUES " +
>
> "(null,null,null,null)");
> sqlStmt.execute();
> sqlStmt = con.prepareStatement("SELECT
> MAX(company_id) FROM company_details");
> ResultSet rs = sqlStmt.executeQuery();
> if (rs.next())
> cId = new Integer(rs.getInt("COMPANY_ID"));
> }
>
> catch (SQLException sqle) {
> throw new EJBException(sqle);
> }
>
> catch (javax.naming.NamingException e) {
> throw new EJBException(e);
> }
>
> finally {
> try {
> if (con!=null)
> con.close();
> }
> catch (SQLException sqle) {
> throw new EJBException(sqle);
> }
> }
>
> this.companyId = cId;
> return cId;
> }
>
> public void ejbPostCreate() {
> }
>
>
> public void ejbActivate() {
> }
>
> public void ejbPassivate() {
> }
>
> public void ejbLoad() {
> }
>
> public void ejbStore() {
> }
>
> public void ejbRemove() {
> }
>
> public void setCompanyAbn(java.lang.String _companyAbn) {
> companyAbn = _companyAbn;
> }
>
> public java.lang.String getCompanyAbn() {
> return companyAbn;
> }
>
> public void setCompanyDesc(java.lang.String _companyDesc) {
> companyDesc = _companyDesc;
> }
> public java.lang.String getCompanyDesc() {
> return companyDesc;
> }
>
> public void setCompanyLogoImg(java.lang.String _companyLogoImg) {
> companyLogoImg = _companyLogoImg;
> }
>
> public java.lang.String getCompanyLogoImg() {
> return companyLogoImg;
> }
>
> public void setCompanyName(java.lang.String _companyName) {
> companyName = _companyName;
> }
>
> public java.lang.String getCompanyName() {
> return companyName;
> }
> }
>
>
>
> Chris Tragas
> -----------------------------------------
> [EMAIL PROTECTED]
> +61 3 9695 5711 direct
> +61 3 0402 28 10 20 mobile
> -----------------------------------------
> a t o m i c m e d i a
> Leading Partners Online
>
> Level 1 / 216 City Road
> Southbank, Melbourne, Vic 3006
> Australia.
>
> +61 3 9695 5777 tel
> +61 3 9695 5700 fax
> -----------------------------------------
> www.atomicmedia.com
> -----------------------------------------
>
>
>
> _______________________________________________
> JBoss-user mailing list
> [EMAIL PROTECTED]
> http://lists.sourceforge.net/lists/listinfo/jboss-user
>
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user