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

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

    osataken

      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;
       try{
       System.out.println("ejbCreate() Called.");
       this.accountID=accountID;
       this.ownerName=ownerName;
       this.balance=0;
       conn=getConnection();
       pstmt=conn.prepareStatement("insert into accounts(id,ownerName,balance) values (?,?,?)");
       pstmt.setString(1,accountID);
       pstmt.setString(2,ownerName);
       pstmt.setDouble(3,balance);
       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

          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);
           this.accountID=accountID;
           this.ownerName=ownerName;
           this.balance=0;
          


          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.

          James

          • 2. Re: why i can't invoke bmp method
            osataken

            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


              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 ?

              James

              • 4. Re: why i can't invoke bmp method
                osataken

                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;
                 try{
                 props.put(
                 InitialContext.INITIAL_CONTEXT_FACTORY,
                 "org.jnp.interfaces.NamingContextFactory");
                 props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
                 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();
                 }
                 else
                 System.out.println("Cannot find account.");
                 System.out.println("Initial Balance="+account.getBalance());
                 account.deposit(100);
                 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();
                 account=null;
                 account=home.findByPrimaryKey(pk);
                 System.out.println("Found account with ID "+pk+" .Balance="+account.getBalance());
                 account.withdraw(150);
                 }catch(Exception e){
                 System.out.println("Caught Exception!!!");
                 e.printStackTrace();
                 }finally{
                 if (account!=null)
                 try {
                 account.remove();
                 } catch (RemoteException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
                 } catch (RemoveException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
                 }
                 }
                 }
                


                when i run Client as application in eclipse the output is

                Total of all account 1200000.0
                Cannot find account.
                Caught Exception!!!
                java.lang.NullPointerException
                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().

                ejb-jar.xml
                <?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">
                <ejb-jar>
                 <display-name>BMPTest</display-name>
                 <enterprise-beans>
                 <entity>
                 <ejb-name>Account</ejb-name>
                 <home>examples.AccountHome</home>
                 <remote>examples.Account</remote>
                 <local-home>examples.AccountLocalHome</local-home>
                 <local>examples.AccountLocal</local>
                 <ejb-class>examples.AccountBean</ejb-class>
                 <persistence-type>Bean</persistence-type>
                 <prim-key-class>examples.AccountPK</prim-key-class>
                 <reentrant>false</reentrant>
                 <resource-ref>
                 <res-ref-name>jdbc/ejbPool</res-ref-name>
                 <res-type>javax.sql.DataSource</res-type>
                 <res-auth>Container</res-auth>
                 </resource-ref>
                 </entity>
                 </enterprise-beans>
                 <assembly-descriptor>
                 <container-transaction>
                 <method>
                 <ejb-name>Account</ejb-name>
                 <method-intf>Remote</method-intf>
                 <method-name>*</method-name>
                 </method>
                 <method>
                 <ejb-name>Account</ejb-name>
                 <method-intf>Local</method-intf>
                 <method-name>*</method-name>
                 </method>
                 <trans-attribute>Required</trans-attribute>
                 </container-transaction>
                 </assembly-descriptor>
                </ejb-jar>
                
                


                jboss.xml
                <?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">
                
                <jboss>
                
                 <unauthenticated-principal>nobody</unauthenticated-principal>
                
                 <enterprise-beans>
                
                 <!--
                 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.
                 -->
                 <entity>
                 <ejb-name>Account</ejb-name>
                 <jndi-name>AccountBean</jndi-name>
                 <local-jndi-name>AccountLocal</local-jndi-name>
                 <resource-ref>
                 <res-ref-name>jdbc/ejbPool</res-ref-name>
                 <jndi-name>java:/ejbPool</jndi-name>
                 </resource-ref>
                 </entity>
                
                 </enterprise-beans>
                
                 <resource-managers>
                 </resource-managers>
                
                </jboss>
                



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

                • 5. Re: why i can't invoke bmp method
                  osataken

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

                  pstmt.executeUpdate();
                  

                  in ejbCreate() so database was not created too.

                  thank you again jamesstrachan.^^

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

                    You found the bug, not me.

                    I missed that one.

                    James