Marcel Schepers wrote:

> Is it allowed to make a internal B2B call inside the ejbCreate
> method. I have a CMP entity bean whose primary key is a Long
> value. The new Long value is stored in a separate table with a
> separate entity bean. While creating the bean I would like to make a
> call to get the new primary key of the bean to be created?

This is how I implemented it:

Sequence Bean:
------------------------

package com.company.system.util.ejb;

import javax.ejb.EntityContext;

public class SequenceBean implements javax.ejb.EntityBean {
        
        transient private EntityContext ctx;
        
        public String tableName;
        public int currentValue;
        public byte increase;
        
        public SequencePK ejbCreate(String tableName, int currentValue, byte
increase) {
                this.tableName = tableName;
                this.currentValue = currentValue;
                this.increase = increase;
                return null;
        }
        
        public void ejbPostCreate(String tableName, int currentValue, byte
increase) {
                // do nothing, not implemented
        }
        
        public int getCurrentValue() { return currentValue; }
        public void setCurrentValue(int value) { currentValue = value; }
        
        public int getNextValue() {
                currentValue += increase;
                return currentValue;
        }
        
        public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }

        public void unsetEntityContext() { ctx = null; }        

        public void ejbActivate() { }   
        public void ejbPassivate() { }  
        public void ejbLoad() { }       
        public void ejbStore() { }      
        public void ejbRemove() { }     
}


Message Bean:
----------------------

package com.company.system.message.ejb;

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.Context;

import com.company.system.util.ejb.*;

public class MessageBean implements EntityBean {
        
        public EntityContext context;
        
        public int messageId;
        public int userId;
        public int fromUserId;
        public String content;
        public boolean newInd;

        public MessagePK ejbCreate(int userId, int fromUserId, String
content) {
                // generate next messageId
                try {
                Context jndiContext = new InitialContext();
        Object obj = jndiContext.lookup("system/Sequence");
        
         SequenceHome home =
(SequenceHome)javax.rmi.PortableRemoteObject.narrow(obj,
SequenceHome.class);
                        
                        SequencePK pk = new SequencePK("MESSAGE");
                        Sequence sequence = home.findByPrimaryKey(pk);
                        
                        messageId = sequence.getNextValue();
                } catch (Exception ex) {
                        ex.printStackTrace();
                        throw new EJBException(ex);
                }

                this.userId = userId;
                this.fromUserId = fromUserId;
                this.content = content;
                this.newInd = true;
                return null;
        }
        
   public int getMessageId() { return messageId; }

   public int getUserId() { return userId; }
   public void setUserId(int userId) { this.userId = userId; }
   
   public int getFromUserId() { return fromUserId; }
   public void setFromUserId(int fromUserId) { this.fromUserId = fromUserId;
}

   public String getContent() { return content; }
   public void setContent(String content) { this.content = content; }

   public boolean getNewInd() { return newInd; }
   public void setNewInd(boolean newInd) { this.newInd = newInd; }

        public void ejbPostCreate(int userId, int fromUserId, String
content) {
                // do nothing, not implemented
        }
        
        public void setEntityContext(EntityContext context) { this.context =
context; }      
        public void unsetEntityContext() { context = null; }    

        public void ejbActivate() { }
        public void ejbPassivate() { }
        public void ejbLoad() { }
        public void ejbStore() { }      
        public void ejbRemove() { }     
}

Creating Syquence table:
------------------------------------

import javax.naming.*;
import javax.rmi.PortableRemoteObject; 

import com.company.system.message.ejb.*;
import com.company.system.util.ejb.*;

public class CreateSequenceTable
{
        public static void main(String[] args)
        {
                System.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
                System.setProperty("java.naming.provider.url",
"localhost:1099");

                try {
                InitialContext jndiContext = new InitialContext();
                        System.out.println("Got context");
        
                        Object sequenceObj  =
jndiContext.lookup("system/Sequence");
                        System.out.println("Got reference to Sequence
Bean");
        
                        SequenceHome sequenceHome = (SequenceHome) 
                                PortableRemoteObject.narrow (sequenceObj,
SequenceHome.class);
        
                        byte increase = 1;
                        int currentValue = 0;
                        
                        Sequence sequence = sequenceHome.create("MESSAGE",
currentValue, increase);
                        System.out.println("Created some sequences");
                }
                catch(Exception e) {
                        System.out.println(e.toString());
                }
        }
}

Regards,
        Leonid.


_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to