6 Replies Latest reply on Sep 5, 2003 4:51 AM by wprusty

    Jboss!Save me! How to config pk auto increment with ORACLE9I

    wprusty

      Hi,all
      I am a Chinese guy.I like jboss very much.I have succesfully configed primary key auto increment with mysql4.0 and Mssqserver 2000 but I cannot config pk auto increment with Oracle9i whether I use jdbc 2.0 driver or jdbc 3.0 driver.I almost want to die because this problem make me into trouble for 5 days .So anybody could tell me how to config pk auto increment with jboss3.2.1 and Oracle9i.(Does jdbc 2.0 driver can not really make pk auto increment with jboss and oracle9i?) And If I use jdbc 3.0 driver,then how can i make pk auto increment with jboss3.2.1 and Oracle9i?? My heart is very very eager because I cannot get help other from here. Please someone must help me. Many many thanks to you all!
      Best Regards,
      Rusty Wang
      ShanDong China

        • 1. Re: Jboss!Save me! How to config pk auto increment with ORAC
          wprusty

          Oh,Poor guy! Anybody can help me? Nobody?

          • 2. Re: Jboss!Save me! How to config pk auto increment with ORAC
            darranl

            What is your problem?

            What errors are you seeing that tell you that it doesn't work?

            Are the tables being created correctly in the database?

            If there are any exceptions being thrown can you post the stack trace.

            Can you post the deployment descriptors for the beans that you are having problems with.

            • 3. Re: Jboss!Save me! How to config pk auto increment with ORAC
              rlaenen

              Don't know your exact problem but if you're trying to use an auto-increment pk with an entity bean and Oracle 9i, this will not work.

              Have a look at the thread named
              "autogenerated pk's with cmp entity beans ?"

              that describes this problem.

              You should implement the auto-increment on the db-level (with db-trigger) and retrieve your pk-value in the postCreate.

              Roger.

              • 4. Re: Jboss!Save me! How to config pk auto increment with ORAC
                wprusty

                First thank you all!
                I use eclipse2.1.1+lomboz plugin and I use xdoclet to generate other class and I use Oranxo jdbc 3 driver as my oracle9i driver.I make a very simple cmp as follow:
                package com.wprusty.autotest;
                import java.rmi.RemoteException;
                import javax.ejb.EJBException;
                import javax.ejb.EntityBean;
                import javax.ejb.EntityContext;
                /**
                * @ejb.bean name="Customer"
                * jndi-name="CustomerBean"
                * type="CMP"
                * primkey-field="id"
                * schema="mySchema"
                * cmp-version="2.x"
                *
                * @ejb.persistence
                * table-name="customer"
                *
                * @ejb.finder
                * query="SELECT OBJECT(a) FROM mySchema as a"
                * signature="java.util.Collection findAll()"
                * @jboss.entity-command name = "get-generated-keys" class = "org.jboss.ejb.plugins.cmp.jdbc.jdbc3.JDBCGetGeneratedKeysCreateCommand"
                *
                **/

                public abstract class CustomerBean implements EntityBean {
                private EntityContext entityContext;

                /**
                * The ejbCreate method.
                *
                * @ejb.create-method
                */
                public java.lang.Integer ejbCreate(String firstName,String lastName) throws javax.ejb.CreateException {
                // EJB 2.0 spec says return null for CMP ejbCreate methods.
                // TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE.
                // setMyField("Something");
                setFirstName(firstName);
                setLastName(lastName);
                return null;
                }

                /**
                * The container invokes this method immediately after it calls ejbCreate.
                *
                */
                public void ejbPostCreate() throws javax.ejb.CreateException {
                }

                /**
                * Returns the id
                * @return the id
                *
                * @ejb.persistent-field
                * @ejb.persistence
                * column-name="id"
                *
                * @ejb.pk-field
                * @ejb.interface-method
                * @jboss.persistence auto-increment = "true"
                */
                public abstract java.lang.Integer getId();

                /**
                * Sets the id
                *
                * @param java.lang.Integer the new id value
                *
                * @ejb.interface-method
                */
                public abstract void setId(java.lang.Integer id);

                /**
                * Returns the firstName
                * @return the firstName
                *
                * @ejb.persistent-field
                * @ejb.persistence
                * column-name="first_name"
                *
                *
                * @ejb.interface-method
                */
                public abstract java.lang.String getFirstName();

                /**
                * Sets the firstName
                *
                * @param java.lang.String the new firstName value
                *
                * @ejb.interface-method
                */
                public abstract void setFirstName(java.lang.String firstName);

                /**
                * Returns the lastName
                * @return the lastName
                *
                * @ejb.persistent-field
                * @ejb.persistence
                * column-name="last_name"
                *
                *
                * @ejb.interface-method
                */
                public abstract java.lang.String getLastName();

                /**
                * Sets the lastName
                *
                * @param java.lang.String the new lastName value
                *
                * @ejb.interface-method
                */
                public abstract void setLastName(java.lang.String lastName);

                /* (non-Javadoc)
                * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
                */
                public void setEntityContext(EntityContext arg0)
                throws EJBException, RemoteException {
                this.entityContext=arg0;

                }

                /* (non-Javadoc)
                * @see javax.ejb.EntityBean#unsetEntityContext()
                */
                public void unsetEntityContext() throws EJBException, RemoteException {
                this.entityContext=null;

                }

                }
                After xdoclet generate the other files,I find jbosscmp-jdbc.xml is wrong ,so I modify this file as follow:
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 3.2//EN"
                "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_2.dtd">
                <jbosscmp-jdbc>

                java:/OracleDS
                <datasource-mapping>Oracle9i</datasource-mapping>
                <preferred-relation-mapping>foreign-key</preferred-relation-mapping>

                <enterprise-beans>
                <!--
                To add beans that you have deployment descriptor info for, add
                a file to your XDoclet merge directory called jbosscmp-jdbc-beans.xml
                that contains the markup for those beans.
                -->

                <ejb-name>Customer</ejb-name>
                <table-name>customer</table-name>
                <cmp-field>
                <field-name>id</field-name>
                <column-name>id</column-name>
                <auto-increment/>
                </cmp-field>
                <cmp-field>
                <field-name>firstName</field-name>
                <column-name>first_name</column-name>
                </cmp-field>
                <cmp-field>
                <field-name>lastName</field-name>
                <column-name>last_name</column-name>
                </cmp-field>
                <entity-command name = "get-generated-keys" class = "org.jboss.ejb.plugins.cmp.jdbc.jdbc3.JDBCGetGeneratedKeysCreateCommand"/>

                </enterprise-beans>
                </jbosscmp-jdbc>
                In oracle ,I make a customer table before.I dont want to use trigger.When I test this bean,I got an error as follow:
                javax.ejb.CreateException: Could not create entity:java.lang.NullPointerException
                at org.jboss.ejb.plugins.cmp.jdbc.jdbc3.JDBCGetGeneratedKeysCreateCommand.insertEntity(JDBCGetGeneratedKeysCreateCommand.java:198)
                at org.jboss.ejb.plugins.cmp.jdbc.jdbc3.JDBCGetGeneratedKeysCreateCommand.execute(JDBCGetGeneratedKeysCreateCommand.java:123)
                at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:569)
                at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:225)
                at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:270)
                at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:725)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContainer.java:998)
                at org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:88)
                at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invokeHome(EntitySynchronizationInterceptor.java:188)
                at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeHome(CachedConnectionInterceptor.java:215)
                at org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:88)
                at org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(EntityInstanceInterceptor.java:91)
                at org.jboss.ejb.plugins.EntityLockInterceptor.invokeHome(EntityLockInterceptor.java:61)
                at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java:28)
                at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:88)
                at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:243)
                at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:74)
                at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:92)
                at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:120)
                at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:93)
                at org.jboss.ejb.EntityContainer.internalInvokeHome(EntityContainer.java:477)
                at org.jboss.ejb.Container.invoke(Container.java:694)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:549)
                at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:359)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
                at sun.rmi.transport.Transport$1.run(Transport.java:148)
                at java.security.AccessController.doPrivileged(Native Method)
                at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
                at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
                at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
                at java.lang.Thread.run(Thread.java:534)
                at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
                at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
                at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
                at org.jboss.invocation.jrmp.server.JRMPInvoker_Stub.invoke(Unknown Source)
                at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvokerProxy.java:135)
                at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:87)
                at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
                at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
                at org.jboss.proxy.ejb.HomeInterceptor.invoke(HomeInterceptor.java:173)
                at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
                at $Proxy0.create(Unknown Source)
                at com.wprusty.autotest.Test.testBean(Test.java:48)
                at com.wprusty.autotest.Test.main(Test.java:65)
                You know I use the same method with mysql 4.0 and jboss 3.2.1(of course I use mysql jdbc 3 driver) It worked perfect fine,so I am very strange.I know how to create a trigger to make pk auto increment and It worked fine but I dont want to use it.Thank you all!

                • 5. Re: Jboss!Save me! How to config pk auto increment with ORAC
                  rlaenen

                  I would check if your Oranxo driver supports the following statement :

                  ps = con.prepareStatement(insertEntitySQL, PreparedStatement.RETURN_GENERATED_KEYS);

                  If not, don't look further, it will not work.
                  If yes, pls let me know.

                  Roger.

                  • 6. Re: Jboss!Save me! How to config pk auto increment with ORAC
                    wprusty

                    Thank you all!
                    I will just use the oracle trigger.No other method.Thank you.