You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Mark Aufdencamp <ma...@aufdencamp.com> on 2007/09/05 06:42:43 UTC

CMP/CMR 2.1 EJBCreate EJBPostCreate

Hi All,

I'm still wondering about control of the SQL INSERT statement when an
EJBCreate is executed.  Specifically, how can it be defined properly to
allow the setting of CMR Relationships on Foreign Key Columns that are
defined as NOT NULL.  JBoss and WebLogic both possess a container
configuration command that effectively delays the SQL INSERT until after
the EJBPostCreate has been executed to specifically allow for this
behavior.

I've removed the NOT NULL constraint on my Database Schema, and it
functions properly with the EJBCreate setting the Primary Key, and the
EJBPostCreate setting the Foreign Key with the CMR Entity.  I've checked
to insure the MySQL tables are defined as InnoDB, so they should support
Transaction Management.  I've attempted to add the open-ejb.xml
configuration command, <enforce-foreign-key-constraints>, as suggested
by David Carew, but sadly this had no apparent effect.

Can this be done or is it an uncovered bust in the 2.1 specs?  

Am I missing something maybe in the Transaction Management realm that I
dont have configured to have this function?

Anyone engaged in the this type of Create/PostCreate with NOT NULL
Foreign Keys on a different database and JDBC driver, say Derby?

Thanks to All for suggestions,

Mark Aufdencamp
Mark@Aufdencamp.com


Re: CMP/CMR 2.1 EJBCreate EJBPostCreate

Posted by David Blevins <da...@visi.com>.
Definitely not covered by the spec -- the effective insert is  
definitely supposed to happen between ejbCreate and ejbPostCreate.   
The CMP implementation is driven by JPA so we're not really the ones  
who execute the SQL.  Though delaying the persist till after the  
ejbPostCreate seems possible.

Dain, any ideas?

-David

On Sep 4, 2007, at 9:42 PM, Mark Aufdencamp wrote:

> Hi All,
>
> I'm still wondering about control of the SQL INSERT statement when an
> EJBCreate is executed.  Specifically, how can it be defined  
> properly to
> allow the setting of CMR Relationships on Foreign Key Columns that are
> defined as NOT NULL.  JBoss and WebLogic both possess a container
> configuration command that effectively delays the SQL INSERT until  
> after
> the EJBPostCreate has been executed to specifically allow for this
> behavior.
>
> I've removed the NOT NULL constraint on my Database Schema, and it
> functions properly with the EJBCreate setting the Primary Key, and the
> EJBPostCreate setting the Foreign Key with the CMR Entity.  I've  
> checked
> to insure the MySQL tables are defined as InnoDB, so they should  
> support
> Transaction Management.  I've attempted to add the open-ejb.xml
> configuration command, <enforce-foreign-key-constraints>, as suggested
> by David Carew, but sadly this had no apparent effect.
>
> Can this be done or is it an uncovered bust in the 2.1 specs?
>
> Am I missing something maybe in the Transaction Management realm  
> that I
> dont have configured to have this function?
>
> Anyone engaged in the this type of Create/PostCreate with NOT NULL
> Foreign Keys on a different database and JDBC driver, say Derby?
>
> Thanks to All for suggestions,
>
> Mark Aufdencamp
> Mark@Aufdencamp.com
>
>


Re: CMP/CMR 2.1 EJBCreate EJBPostCreate

Posted by Efren Gonzalez <go...@tutopia.com>.
Hello

I have been able to have 2.1 CMP Entity EJB's with CMR working with 
Websphere Community Edition 1.0 (which is Geronimo 1.0 anyway), MySQL 
5.0 InnoDB tables and NOT NULL constraints, I think this is exactly what 
you want to accomplish.

I'm not sure what may be missing so I'll post a complete example, this 
is actually taken from a working application (Interfaces and non 
relevant code ommited).

--- START ejb-jar.xml ---
<enterprise-beans>
<entity>
<ejb-name>Tipo</ejb-name>
<local-home>com.mx.jlr.crm.ejb.entity.tipo.TipoLocalHome</local-home>
<local>com.mx.jlr.crm.ejb.entity.tipo.TipoLocal</local>
<ejb-class>com.mx.jlr.crm.ejb.entity.tipo.TipoBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>false</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>tipos</abstract-schema-name>

<cmp-field>
<field-name>idTipo</field-name>
</cmp-field>
<cmp-field>
<field-name>idMarca</field-name>
</cmp-field>
<cmp-field>
<field-name>nombreTipo</field-name>
</cmp-field>
<primkey-field>idTipo</primkey-field>
</entity>

<entity>
<ejb-name>Marca</ejb-name>
   <local-home>com.mx.jlr.crm.ejb.entity.marca.MarcaLocalHome</local-home>
<local>com.mx.jlr.crm.ejb.entity.marca.MarcaLocal</local>
<ejb-class>com.mx.jlr.crm.ejb.entity.marca.MarcaBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>false</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>marcas</abstract-schema-name>

<cmp-field>
<field-name>idMarca</field-name>
</cmp-field>
<cmp-field>
<field-name>nombreMarca</field-name>
</cmp-field>
<primkey-field>idMarca</primkey-field>
</entity>
</enterprise-beans>

<relationships>
<ejb-relation>
<ejb-relation-name>marcas-tipos</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>marcas</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>Marca</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>tipos</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>tipos</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<relationship-role-source>
<ejb-name>Tipo</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>marca</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Marca</ejb-name>
<method-name>*</method-name>
</method>
<method>
<ejb-name>Tipo</ejb-name>
<method-name>*</method-name>
</method>

<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
--- END ejb-jar.xml ---

--- START openejb-jar.xml ---
<enforce-foreign-key-constraints />

<enterprise-beans>
<entity>
<ejb-name>Marca</ejb-name>
<local-jndi-name>ejb/Marca</local-jndi-name>
<table-name>marcas</table-name>
<cmp-field-mapping>
<cmp-field-name>idMarca</cmp-field-name>
<table-column>id_marca</table-column>
</cmp-field-mapping>
<cmp-field-mapping>
<cmp-field-name>nombreMarca</cmp-field-name>
<table-column>nombre_marca</table-column>
</cmp-field-mapping>
<pkgen:key-generator>
<pkgen:sequence-table>
<pkgen:table-name>sequence_table</pkgen:table-name>
<pkgen:sequence-name>Marca</pkgen:sequence-name>
<pkgen:batch-size>1</pkgen:batch-size>
</pkgen:sequence-table>
</pkgen:key-generator>
</entity>

<entity>
<ejb-name>Tipo</ejb-name>
<jndi-name>ejb/Tipo</jndi-name>
<table-name>tipos</table-name>
<cmp-field-mapping>
<cmp-field-name>idTipo</cmp-field-name>
<table-column>id_tipo</table-column>
</cmp-field-mapping>
<cmp-field-mapping>
<cmp-field-name>idMarca</cmp-field-name>
<table-column>id_marca</table-column>
</cmp-field-mapping>
<cmp-field-mapping>
<cmp-field-name>nombreTipo</cmp-field-name>
<table-column>nombre_tipo</table-column>
</cmp-field-mapping>
<pkgen:key-generator>
<pkgen:sequence-table>
<pkgen:table-name>sequence_table</pkgen:table-name>
<pkgen:sequence-name>Tipo</pkgen:sequence-name>
<pkgen:batch-size>1</pkgen:batch-size>
</pkgen:sequence-table>
</pkgen:key-generator>
</entity>
</enterprise-beans>

<relationships>
<ejb-relation>
<ejb-relation-name>marcas-tipos</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>marcas</ejb-relationship-role-name>
<relationship-role-source>
<ejb-name>Marca</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>tipos</cmr-field-name>
</cmr-field>
<role-mapping>
<cmr-field-mapping>
<key-column>id_marca</key-column>
<foreign-key-column>id_marca</foreign-key-column>
</cmr-field-mapping>
</role-mapping>
</ejb-relationship-role>
</ejb-relation>

<ejb-relation>
<ejb-relation-name>tipos-marcas</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>tipos</ejb-relationship-role-name>
<relationship-role-source>
<ejb-name>Tipo</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>marca</cmr-field-name>
</cmr-field>
<foreign-key-column-on-source />
<role-mapping>
<cmr-field-mapping>
<key-column>id_marca</key-column>
<foreign-key-column>id_marca</foreign-key-column>
</cmr-field-mapping>
</role-mapping>
</ejb-relationship-role>
</ejb-relation>
</relationships>
--- END openejb-jar.xml ---

--- START MarcaBean.java ---
public abstract class MarcaBean implements EntityBean {
   public abstract void setIdMarca(Integer idMarca);
   public abstract Integer getIdMarca();
   public abstract void setNombreMarca(String nombreMarca);
   public abstract String getNombreMarca();
   public abstract void setTipos(Collection tipos);
   public abstract Collection getTipos();

   public Integer ejbCreate(String nombreMarca) throws CreateException {
     setNombreMarca(nombreMarca);

     return null;
   }

   public void ejbPostCreate(String nombreMarca) {
     // Nada
   }
}
--- END MarcaBean.java ---

--- START TipoBean.java ---
public abstract class TipoBean implements EntityBean {
   public abstract void setIdTipo(Integer idTipo);
   public abstract Integer getIdTipo();
   public abstract void setMarca(MarcaLocal marca);
   public abstract MarcaLocal getMarca();
   public abstract void setIdMarca(Integer idMarca);
   public abstract Integer getIdMarca();
   public abstract void setNombreTipo(String nombreTipo);
   public abstract String getNombreTipo();

   public Integer ejbCreate(MarcaLocal marca, String nombreTipo) throws 
CreateException {
     setNombreTipo(nombreTipo);

     return null;
   }

   public void ejbPostCreate(MarcaLocal marca, String nombreTipo) {
     setMarca(marca);
   }
}
--- END TipoBean.java ---

--- START Marcas.sql ---
CREATE TABLE `marcas` (
   `id_marca` int(10) unsigned NOT NULL,
   `nombre_marca` varchar(25) NOT NULL,
   PRIMARY KEY  (`id_marca`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- END Marcas.sql ---

--- START Tipos.sql ---
CREATE TABLE `tipos` (
   `id_tipo` int(10) unsigned NOT NULL,
   `id_marca` int(10) unsigned NOT NULL,
   `nombre_tipo` varchar(25) NOT NULL,
   PRIMARY KEY  (`id_tipo`),
   KEY `IDX_MARCA` (`id_marca`),
   CONSTRAINT `FK_MARCAS_TIPOS` FOREIGN KEY (`id_marca`) REFERENCES 
`marcas` (`id_marca`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- END Tipos.sql ---

I think I'm not missing anything, at least I don't remember making any 
special configuration.

Hope this helps...

Regards,
Efrén

Mark Aufdencamp escribió:
> Hi All,
> 
> I'm still wondering about control of the SQL INSERT statement when an
> EJBCreate is executed.  Specifically, how can it be defined properly to
> allow the setting of CMR Relationships on Foreign Key Columns that are
> defined as NOT NULL.  JBoss and WebLogic both possess a container
> configuration command that effectively delays the SQL INSERT until after
> the EJBPostCreate has been executed to specifically allow for this
> behavior.
> 
> I've removed the NOT NULL constraint on my Database Schema, and it
> functions properly with the EJBCreate setting the Primary Key, and the
> EJBPostCreate setting the Foreign Key with the CMR Entity.  I've checked
> to insure the MySQL tables are defined as InnoDB, so they should support
> Transaction Management.  I've attempted to add the open-ejb.xml
> configuration command, <enforce-foreign-key-constraints>, as suggested
> by David Carew, but sadly this had no apparent effect.
> 
> Can this be done or is it an uncovered bust in the 2.1 specs?  
> 
> Am I missing something maybe in the Transaction Management realm that I
> dont have configured to have this function?
> 
> Anyone engaged in the this type of Create/PostCreate with NOT NULL
> Foreign Keys on a different database and JDBC driver, say Derby?
> 
> Thanks to All for suggestions,
> 
> Mark Aufdencamp
> Mark@Aufdencamp.com
> 
> 
> 
>