6 Replies Latest reply on Aug 20, 2004 3:59 PM by jamesstrachan

    why i can't invoke bmp method "ejbCreate()"

    Kunarak Osatapirat Newbie

      from mastering ejb II
      i 've created primary key class (AccountPK) and defined it in <primary-key class> tag in ejb-jar.xml .

      this is my EJBHome

      public interface AccountHome extends EJBHome {
       Account create(String accountID,String ownerName) throws CreateException,RemoteException;
       public Account findByPrimaryKey(AccountPK key) throws FinderException,RemoteException;
       public Collection findByOwnerName(String name) throws FinderException,RemoteException;
       public double getTotalBankValue() throws AccountException,RemoteException;

      and implement method (ejbCreate) in EntityBean

      public AccountPK ejbCreate(String accountID,String ownerName) throws CreateException{
       PreparedStatement pstmt=null;
       Connection conn=null;
       System.out.println("ejbCreate() Called.");
       pstmt=conn.prepareStatement("insert into accounts(id,ownerName,balance) values (?,?,?)");
       return new AccountPK(accountID);
       }catch(Exception e){
       throw new CreateException(e.toString());

      i can deploy and test method that implement EJBObject and method that implement EJBHome (except method that return type=AccountPK)

      does it need to config something in jboss.xml or i miss somethings???
      thank you....

        • 1. Re: why i can't invoke bmp method
          jamesstrachan Newbie

          Although you are returning an AccountPK object from your ejbCreate() method, you are not storing an AccountPK object inside the Entity Bean itself.

          So JBoss cannot locate or handle the Entity Bean.

          You need to add as shown below :-

           System.out.println("ejbCreate() Called.");
           // Line added below !
           this.accountKey = new AccountPK(accountId);

          where accountKey is an object of class AccountPK and is referred to in ejb-jar.xml.

          Alternatively, use accountId, which is a String, as the primary key. You only need a primary key class where you have a composite key containing more than one attribute.

          In other words, if your key was account ID + account Type, you would need a primary key class containing both attributes. Since you only have one attribute, you can use accountId as the primary key.


          • 2. Re: why i can't invoke bmp method
            Kunarak Osatapirat Newbie

            thank you for the answer jamesstrachan
            but when i run client i cannot see "ejbCreate() Called" in Server Console and no information insert in database , i think the method "ejbCreate()" was not invoked.

            did i miss something???

            (my English skill is not very well) sorry, if it has some mistakes.^^

            • 3. Re: why i can't invoke bmp method
              jamesstrachan Newbie

              Are you getting any errors when you deploy the jar file ?

              If you get any messages similar to :-

              Instance of primary key not located

              that would indicate a problem.

              And are you getting any error messages or stack traces when you call the ejbCreate() method ?


              • 4. Re: why i can't invoke bmp method
                Kunarak Osatapirat Newbie

                no error occur while i deploy ejb module.
                this is an output when i deploy ejb module(i test in jboss 3.2.3 and 3.2.5)

                00:00:07,066 INFO [EjbModule] Deploying Account
                00:00:07,657 INFO [EJBDeployer] Deployed: file:/C:/jboss-3.2.5/server/default/deploy/Account.jar

                this is Client code

                public class AccountBeanTest {
                 public static void main(String[] args) throws NamingException {
                 Hashtable props = new Hashtable();
                 Account account=null;
                 props.put(InitialContext.PROVIDER_URL, "jnp://");
                 InitialContext c = new InitialContext(props);
                 AccountHome home= (AccountHome) c.lookup("AccountBean");
                 System.out.println("Total of all account "+home.getTotalBankValue());
                 home.create("123-456-7890","John Smith");
                 Iterator i=home.findByOwnerName("John Smith").iterator();
                 if (i.hasNext()){
                 account=(Account) i.next();
                 System.out.println("Cannot find account.");
                 System.out.println("Initial Balance="+account.getBalance());
                 System.out.println("After Deposit 100="+account.getBalance());
                 System.out.println("Total of all accounts in bank now="+home.getTotalBankValue());
                 AccountPK pk=(AccountPK) account.getPrimaryKey();
                 System.out.println("Found account with ID "+pk+" .Balance="+account.getBalance());
                 }catch(Exception e){
                 System.out.println("Caught Exception!!!");
                 if (account!=null)
                 try {
                 } catch (RemoteException e1) {
                 // TODO Auto-generated catch block
                 } catch (RemoveException e1) {
                 // TODO Auto-generated catch block

                when i run Client as application in eclipse the output is

                Total of all account 1200000.0
                Cannot find account.
                Caught Exception!!!
                at examples.AccountBeanTest.main(AccountBeanTest.java:43)

                it shown cannot find account cause ejbCreate() was not invoked.
                Total of all account are selected from database and sum it.

                and in server console the output is
                00:02:39,655 INFO [STDOUT] New AccountBean Created.
                00:02:39,695 INFO [STDOUT] setEntityContext() Called.
                00:02:39,705 INFO [STDOUT] ejbHomeGetTotalBankValue()
                00:02:39,966 INFO [STDOUT] DB CONNECTED
                00:02:40,046 INFO [STDOUT] ejbCreate() Called.
                00:02:40,086 INFO [STDOUT] ejbStore() Called.
                00:02:40,166 INFO [STDOUT] New AccountBean Created.
                00:02:40,166 INFO [STDOUT] setEntityContext() Called.
                00:02:40,166 INFO [STDOUT] ejbFindByOwnerName(John Smith) Called.

                "ejbCreate() Called" not shown after setEntityContext().

                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "ejb-jar_2_0.dtd">

                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">
                 To add beans that you have deployment descriptor info for, add
                 a file to your XDoclet merge directory called jboss-beans.xml that contains
                 the <session></session>, <entity></entity> and <message-driven></message-driven>
                 markup for those beans.

                did i miss somethings????T^T
                thank you

                • 5. Re: why i can't invoke bmp method
                  Kunarak Osatapirat Newbie

                  oh , sorry ejbCreate() was invoked.(i just see it in my last reply - -")
                  i miss a very simple statement in ejbCreate().
                  i miss


                  in ejbCreate() so database was not created too.

                  thank you again jamesstrachan.^^

                  • 6. Re: why i can't invoke bmp method
                    jamesstrachan Newbie

                    You found the bug, not me.

                    I missed that one.