7 Replies Latest reply on Sep 12, 2002 2:44 PM by zemaria

    Problem home.create() for an Entity with relationship 1:N

    zemaria

      Hello.

      I have created a relationship 1:N and when a try to create (InterfaceHome.create()) an Entity Bean the side 1 i get the follow error message in jboss console:

      00:44:50,241 ERROR [lojaVirtual/Produto] Could not create entity
      java.sql.SQLException: [Microsoft][SQLServer JDBC Driver][SQLServer]Column name 'categoria' appears
      more than once in the result column list.
      at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
      at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
      at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
      at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
      at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
      at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
      at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
      at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
      at com.microsoft.jdbc.base.BaseStatement.executeInternal(Unknown Source)
      at com.microsoft.jdbc.base.BasePreparedStatement.execute(Unknown Source)
      at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
      at org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement.executeUpdate(LocalPreparedS
      tatement.java:308)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.insertEntity(JDBCCreateEntityComma
      nd.java:196)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.execute(JDBCCreateEntityCommand.ja
      va:131)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:450)
      at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:253)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConne
      ctionInterceptor.java:270)
      at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:728)
      at java.lang.reflect.Method.invoke(Native Method)
      at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContainer.java:1116)
      at org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:73)
      at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invokeHome(EntitySynchronizationIn
      terceptor.java:257)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeHome(CachedConnect
      ionInterceptor.java:215)
      at org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(EntityInstanceInterceptor.java
      :134)
      at org.jboss.ejb.plugins.EntityLockInterceptor.invokeHome(EntityLockInterceptor.java:79)
      at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java
      :44)
      at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:98)
      at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:167)
      at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:52)
      at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:104)
      at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:109)
      at org.jboss.ejb.EntityContainer.invokeHome(EntityContainer.java:487)
      at org.jboss.ejb.Container.invoke(Container.java:726)
      at org.jboss.ejb.EntityContainer.invoke(EntityContainer.java:1055)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:491)
      at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:362)
      at java.lang.reflect.Method.invoke(Native Method)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:241)
      at sun.rmi.transport.Transport$1.run(Transport.java:152)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:148)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:465)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:706)
      at java.lang.Thread.run(Thread.java:484)

      [ejb-jar.xml]
      ...
      <!-- Relationships -->

      <ejb-relation >
      <ejb-relation-name>categoria-produto</ejb-relation-name>
      <!-- bidirectional -->
      <ejb-relationship-role >
      <ejb-relationship-role-name>produto</ejb-relationship-role-name>
      One
      <relationship-role-source >
      <ejb-name>lojaVirtual/CategoriaProduto</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>produtos</cmr-field-name>
      <cmr-field-type>java.util.Set</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role >
      <ejb-relationship-role-name>categoria</ejb-relationship-role-name>
      Many
      <relationship-role-source >
      <ejb-name>lojaVirtual/Produto</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>categoria</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>

      ...

      [jbosscmp-jdbc.xml]
      ...

      <ejb-relation>
      <ejb-relation-name>categoria-produto</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>produto</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>codigo</field-name>
      <column-name>categoria</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>categoria</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>

      ...

      [SQL Server Script]
      CREATE TABLE [dbo].[CategoriaProduto] (
      [codigo] [numeric](3, 0) NOT NULL ,
      [Descricao] [varchar] (50) NOT NULL
      ) ON [PRIMARY]
      GO
      ALTER TABLE [dbo].[CategoriaProduto] WITH NOCHECK ADD
      CONSTRAINT [PK_CategoriaProduto] PRIMARY KEY CLUSTERED
      (
      [codigo]
      ) ON [PRIMARY]

      CREATE TABLE [dbo].[Produto] (
      [codigo] [numeric](5, 0) NOT NULL ,
      [descricao] [varchar] (50) NOT NULL ,
      [preco] [numeric](5, 2) NOT NULL ,
      [categoria] [numeric](3, 0) NOT NULL
      ) ON [PRIMARY]
      GO
      ALTER TABLE [dbo].[Produto] WITH NOCHECK ADD
      CONSTRAINT [PK_Produto] PRIMARY KEY CLUSTERED
      (
      [codigo]
      ) ON [PRIMARY]
      GO
      ALTER TABLE [dbo].[Produto] ADD
      CONSTRAINT [FK_Produto_CategoriaProduto] FOREIGN KEY
      (
      [categoria]
      ) REFERENCES [dbo].[CategoriaProduto] (
      [codigo]
      ) ON DELETE CASCADE
      GO


      I have tested the relationship and they are work nice.

      Sorry about my english. I am a brazilian.

        • 1. Re: Problem home.create() for an Entity with relationship 1:
          dsundstrom

          It looks like you are trying to do one of the things currently not supported by JBossCMP. The following is not supported:

          NOT NULL foreign key
          Foreigh key as part of a primary key
          CMP field that maps to the same colum as a CMR foreign key

          If you are doing any of the above, you will have to change it to use JBoss (or add the required features to JBoss).

          • 2. Re: Problem home.create() for an Entity with relationship 1:
            zemaria

            Thanks.
            But the create method of Home Interface receive the foreign key field as parameter and the ejbCreate set its value.

            "codigoCategoria" is the foreign key field.

            Home Interface
            ...
            public br.com.easyNet.lojaVirtual.ejb.produto.Produto create(java.lang.String codigo,java.lang.String descricao,java.lang.String codigoCategoria,java.lang.Float preco) throws java.rmi.RemoteException,javax.ejb.CreateException;
            ...
            Bean Class
            ...
            /**
            * @ejb:create-method view-type="both"
            **/
            public ProdutoPK ejbCreate (String codigo, String descricao,
            String codigoCategoria, Float preco)
            throws CreateException {
            setCodigo(codigo);
            setDescricao(descricao);
            setCodigoCategoria(codigoCategoria);
            setPreco(preco);
            return null;
            }

            I put the set e get to the foreign key field.
            /**
            * @ejb:persistent-field
            * @jboss:column-name name="categoria"
            * @ejb:interface-method view-type="both"
            **/
            public abstract String getCodigoCategoria ();
            public abstract void setCodigoCategoria (String codigoCategoria);

            • 3. Re: Problem home.create() for an Entity with relationship 1:
              zemaria

              The foreign key is not part of the primary key.

              CategoriaProduto - Primary Key: codigo

              Produto - Primary Key: codigo

              CategoriaProduto 1..N Produto

              Produto - foreign key (categoria)

              • 4. Re: Problem home.create() for an Entity with relationship 1:
                dsundstrom

                I'm sorry you lost me.

                The original exception is almost always caused by having a cmp-field and cmr-field mapped to the same colum, which is not supported. If you are doing this, you'll need to change it. If you are not, double check your jbosscmp-jdbc.xml file to see if XDoclet did.

                • 5. Re: Problem home.create() for an Entity with relationship 1:
                  zemaria

                  When this things will be support ?

                  Where can a see what is support or not ?

                  I think that is a serious limitation in CMP 2.0

                  I am trying migrate from weblogic to jboss, but it is very dificult. My projetcs need this thinks.

                  Can you send me a functional example using XDoclet that implement relatioinship 1:N bidiretional ?

                  I will be very glad

                  • 6. Re: Problem home.create() for an Entity with relationship 1:
                    dsundstrom

                    > When this things will be support ?

                    Jboss 4.0 Most likely around JavaOne next year, but could be sooner.

                    > Where can a see what is support or not ?

                    We only agree to support the spec. Everything else is a feature. We don't have an anti-feature list.

                    > I think that is a serious limitation in CMP 2.0

                    I agree.

                    > I am trying migrate from weblogic to jboss, but it is
                    > very dificult. My projetcs need this thinks.

                    You can change you database structure. It is you choice.

                    > Can you send me a functional example using XDoclet
                    > that implement relatioinship 1:N bidiretional ?

                    Sure. You will need XDoclet from the XDoclet CVS HEAD as the release is broken in this area.

                    • 7. Re: Problem home.create() for an Entity with relationship 1:
                      zemaria

                      OK. Thanks for you attention.
                      I will go to continue trying :)