我建议的方式是:你的这个不要写在ejbCreate()中,最好还是放到sessionBean中,然后调用你自己的一个自增bean

解决方案 »

  1.   

    我的一位老师在设计数据表时,将编号类型(我们一般用长整型)的主键id,全部设置为varchar,当时我不明白他为什么放着数据库设计好的自动增量不用,而要自己手动搞主键的自加,原来道理在此!
      

  2.   

    我现在是用BMP做的,  我在BMP中只实现了ejbCreate()。
        如果用Session Bean ,是否是用Stateful。
     
     还有,我的JBoss服务器会出现这个问题。  请问其他服务器也有这个问题吗?
     听说 hibernate实现O-R映射不会出现此问题!
      

  3.   

    人家不是告诉你了么?调用一个自增bean,返回当前要插入数据的主键。当然这个自增bean返回的可能是自增的,也可能跳过一些值的。不过决不可能出现重复!呵呵
    session bean 中
     public Integer insertRecord(MaterialInfo material,AdditionalInfo additional,
                ArrayList appends,SourceInfo source){
          //   here we will insert the data to database:infodb
          //   It's may need the operation of transaction,however,we should
          //   deal with it later in the form of mature product.
      
             if (additional != null) {
                 //insert the record of additional.
                 //we must first declare the necessary parameters to be put into ejbCreate.
                 try {
                     qBTb_Additional = qBTb_AdditionalHome.create(additional.
                             getAcqPerson(),
                             additional.getIndexLevel(), additional.getStatus());
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
                 qBTb_Additional.setAcquirePersonAdv(additional.getAcqPersonAdv());
                 qBTb_Additional.setCheckAdv(additional.getCheckAdv());
                 qBTb_Additional.setCheckPerson(additional.getCheckPerson());
                 qBTb_Additional.setCheckTime(additional.getCheckTime());
                 qBTb_Additional.setMark(additional.getMark());
                 qBTb_Additional.setReferenceAccount(additional.
                         getReferenceAccount());
                 qBTb_Additional.setSubCheckTime(additional.getSubCheckTime());         }
    entity bean 中
     public Integer ejbCreate(String acqPerson,String indexLevel,String status){
        Integer id=null;
        try{
        this.setAcquirePerson(acqPerson);
        this.setIndexLevel(indexLevel);
        this.setStatus(status);    Context context = new InitialContext();
        SequenceSessionInfoLocalHome ahome=(SequenceSessionInfoLocalHome)
           context.lookup("SequenceSessionInfoLocal");    SequenceSessionInfoLocal aSequence=ahome.create();
        //accept next key
        id=new Integer(aSequence.getNextNumberInSequence("QBTbAdditional"));
        this.setAdditionalID(id);    }catch(Exception o){o.printStackTrace();}    return id;
      }//ejbCreate
    至于自增bean如何写,请参考一下J2EE 设计模式等书籍。后边是附带代码的。