wood burning stoves 2.0*
The moose likes JBoss/WildFly and the fly likes JBoss Primary Key Generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "JBoss Primary Key Generation" Watch "JBoss Primary Key Generation" New topic
Author

JBoss Primary Key Generation

Nura Horne
Ranch Hand

Joined: Jul 26, 2001
Posts: 40
Hello,

I am having trouble getting JBoss to create primary keys with the UUIDKeyGeneratorFactory (I tried with MySQL generate keys with no luck also). I get the following exception when I try to create a new Share bean:

javax.ejb.CreateException: Could not create entity:java.sql.SQLException: Column 'id' cannot be null at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:327)

If I explictly call the setId() method in ejbCreate everything works fine and row is inserted in my table... any help would be much appreciated. I have included relevant files below:


ShareBean.java

package tutorial.ejb;

import java.rmi.RemoteException;
import java.util.Date;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;

/**
* @ejb.bean name="Share"
* display-name="Name for Share"
* description="Description for Share"
* local-jndi-name = "ejb/ShareLocal"
* cmp-version="2.x"
* view-type="local"
* primkey-field="id"
* schema="shares"
*
* @jboss.persistence datasource = "java:/MySqlDS"
* @jboss.persistence datasource-mapping = "mySQL"
* @jboss.persistence create-table = "true"
* @jboss.persistence remove-table = "true"
*
* @ejb.finder query="SELECT OBJECT(o) FROM shares as o"
* signature="java.util.Collection findAll()"
*
* @jboss.unknown-pk class="String" column-name = "id" jdbc-type = "String" sql-type = "VARCHAR"
* @jboss.entity-command name="key-generator" class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCKeyGeneratorCreateCommand"
* @jboss.entity-command-attribute name = "key-generator-factory" value = "UUIDKeyGeneratorFactory"
*/
public abstract class ShareBean implements EntityBean {

public ShareBean() {
super();
// TODO Auto-generated constructor stub
}

public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

public void ejbLoad() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

public void ejbRemove() throws RemoveException, EJBException, RemoteException {
// TODO Auto-generated method stub

}

public void ejbStore() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

public void setEntityContext(EntityContext ctx)
throws EJBException,
RemoteException {
// TODO Auto-generated method stub

}

public void unsetEntityContext() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

/**
* Create method
* @ejb.create-method view-type = "local"
*/
public String ejbCreate(String symbol, Date date, Integer quantity, Double price, Double commission) throws javax.ejb.CreateException {
// TODO Auto-generated method stub

setSymbol(symbol);
setDate(date);
setQuantity(quantity);
setPrice(price);
setCommission(commission);

return null;
}
/**
* Post Create method
*/
public void ejbPostCreate(String symbol, Date date, Integer quantity, Double price, Double commission) throws javax.ejb.CreateException {
// TODO Auto-generated method stub

}
/**
* Getter for CMP Field id
*
* @ejb.pk-field
* @ejb.persistence column-name = "id"
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public abstract String getId();

/**
* Setter for CMP Field id
*
* @ejb.interface-method view-type="local"
*/
public abstract void setId(String value);

/**
* Getter for CMP Field symbol
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public abstract String getSymbol();

/**
* Setter for CMP Field symbol
*
* @ejb.interface-method view-type="local"
*/
public abstract void setSymbol(String value);

/**
* Getter for CMP Field date
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public abstract java.util.Date getDate();

/**
* Setter for CMP Field date
*
* @ejb.interface-method view-type="local"
*/
public abstract void setDate(java.util.Date value);

/**
* Getter for CMP Field quantity
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public abstract java.lang.Integer getQuantity();

/**
* Setter for CMP Field quantity
*
* @ejb.interface-method view-type="local"
*/
public abstract void setQuantity(java.lang.Integer value);

/**
* Getter for CMP Field price
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public abstract java.lang.Double getPrice();

/**
* Setter for CMP Field price
*
* @ejb.interface-method view-type="local"
*/
public abstract void setPrice(java.lang.Double value);

/**
* Getter for CMP Field commission
*
*
* @ejb.persistent-field
* @ejb.interface-method view-type="local"
*/
public abstract java.lang.Double getCommission();

/**
* Setter for CMP Field commission
*
* @ejb.interface-method view-type="local"
*/
public abstract void setCommission(java.lang.Double value);

}





ejb-jar.xml


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

<ejb-jar >

<description><![CDATA[No Description.]]></description>
<display-name>Generated by XDoclet</display-name>

<enterprise-beans>

<!-- Session Beans -->
<session >
<description><![CDATA[Description for Fibo]]></description>
<display-name>Name for Fibo</display-name>

<ejb-name>Fibo</ejb-name>

<home>tutorial.interfaces.FiboHome</home>
<remote>tutorial.interfaces.Fibo</remote>
<ejb-class>tutorial.ejb.FiboBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

</session>

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

<!-- Entity Beans -->
<entity >
<description><![CDATA[Description for Share]]></description>
<display-name>Name for Share</display-name>

<ejb-name>Share</ejb-name>

<local-home>tutorial.interfaces.ShareLocalHome</local-home>
<local>tutorial.interfaces.ShareLocal</local>

<ejb-class>tutorial.ejb.ShareBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>shares</abstract-schema-name>
<cmp-field >
<description><![CDATA[Getter for CMP Field id]]></description>
<field-name>id</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[Getter for CMP Field symbol]]></description>
<field-name>symbol</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[Getter for CMP Field date]]></description>
<field-name>date</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[Getter for CMP Field quantity]]></description>
<field-name>quantity</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[Getter for CMP Field price]]></description>
<field-name>price</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[Getter for CMP Field commission]]></description>
<field-name>commission</field-name>
</cmp-field>
<primkey-field>id</primkey-field>

<query>
<query-method>
<method-name>findAll</method-name>
<method-params>
</method-params>
</query-method>
<ejb-ql><![CDATA[SELECT OBJECT(o) FROM shares as o]]></ejb-ql>
</query>
<!-- Write a file named ejb-finders-ShareBean.xml if you want to define extra finders. -->
</entity>

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

<!-- Message Driven Beans -->
<!--
To add message driven beans that you have deployment descriptor info for, add
a file to your XDoclet merge directory called message-driven-beans.xml that contains
the <message-driven></message-driven> markup for those beans.
-->

</enterprise-beans>

<!-- Relationships -->

<!-- Assembly Descriptor -->
<assembly-descriptor >
<!--
To add additional assembly descriptor info here, add a file to your
XDoclet merge directory called assembly-descriptor.xml that contains
the <assembly-descriptor></assembly-descriptor> markup.
-->

<!-- finder permissions -->

<!-- finder permissions -->

<!-- transactions -->

<!-- finder transactions -->
</assembly-descriptor>

</ejb-jar>




jbosscmp-jdbc.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN" "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">

<jbosscmp-jdbc>
<defaults>
</defaults>

<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 <entity></entity> markup for those beans.
-->

<entity>
<ejb-name>Share</ejb-name>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>true</create-table>
<remove-table>true</remove-table>

<cmp-field>
<field-name>id</field-name>
<column-name>id</column-name>

</cmp-field>
<cmp-field>
<field-name>symbol</field-name>

</cmp-field>
<cmp-field>
<field-name>date</field-name>

</cmp-field>
<cmp-field>
<field-name>quantity</field-name>

</cmp-field>
<cmp-field>
<field-name>price</field-name>

</cmp-field>
<cmp-field>
<field-name>commission</field-name>

</cmp-field>

<unknown-pk>
<unknown-pk-class>String</unknown-pk-class>
<column-name>id</column-name>
<jdbc-type>String</jdbc-type>
<sql-type>VARCHAR</sql-type>
</unknown-pk>
<entity-command name="key-generator" class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCKeyGeneratorCreateCommand">
<attribute name="key-generator-factory">UUIDKeyGeneratorFactory</attribute>
</entity-command>
<!-- jboss 3.2 features -->
<!-- optimistic locking does not express the exclusions needed -->
</entity>

</enterprise-beans>

</jbosscmp-jdbc>
Sivakumar Nachimuthu
Ranch Hand

Joined: Aug 10, 2004
Posts: 52
I have written a custom implementation for Primary Key Generation using entity command. This implementation works fine for entity beans having Integer as Primary Key Class. The Class Cast Exception is being thrown for the beans having java.lang.Long as primary key Class.

I have the following ejb doclet code in my bean

code:



* @jboss.entity-command name="my_key_gen"
* @jboss.entity-command-attribute
* name="sequence"
* value="eventid"
*
* @jboss.unknown-pk
* class="java.lang.Long"
* column-name="eventid"
* jdbc-type="LONG"
* sql-type="LONG"
* auto-increment="true"





The exception is thrown when i call local.getEventId() method .


code:



java.lang.ClassCastException
at EventBean$Proxy.getEventid(<generated>
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.invocation.Invocation.performCall(Invocation.java:345)
at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityContainer.java:1174)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.jav
a:72)
at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntitySynchronizationInterc
eptor.java:273)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionI
nterceptor.java:185)
at org.jboss.ejb.plugins.EntityReentranceInterceptor.invoke(EntityReentranceInterceptor.java
:111)
at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(EntityInstanceInterceptor.java:242
)
at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:89)
at org.jboss.ejb.plugins.EntityCreationInterceptor.invoke(EntityCreationInterceptor.java:53)

at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:48)

at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:105)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:313)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:146)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:122)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:192)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.
java:122)
at org.jboss.ejb.EntityContainer.internalInvoke(EntityContainer.java:514)
at org.jboss.ejb.Container.invoke(Container.java:870)
at org.jboss.ejb.plugins.local.BaseLocalProxyFactory.invoke(BaseLocalProxyFactory.java:413)
at org.jboss.ejb.plugins.local.EntityProxy.invoke(EntityProxy.java:44)
at $Proxy554.getEventid(Unknown Source)






Where should i specify the primary class as java.lang.Long to avoid this exception.

Thanks
Siva
Sivakumar Nachimuthu
Ranch Hand

Joined: Aug 10, 2004
Posts: 52
Hi All,
The above issue is solved. It was the mistake on my custom code.
Now i have a problem in Multi- Threaded Environment. I have multiple threads running at the same time to collect the data from different source and store it the table using ejb create method. When i use Primary key generation through entity commands , i am getting duplicate keys generated. How to synchronize the way the keys are generated?

Thanks
Siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JBoss Primary Key Generation
 
Similar Threads
EJB - CMP database connection problem
JBOSS EJBQL generate wrong SQL
Can't deploy EJB to JBOSS after adding '@ejb.finder' clause
Error in EJB2.0 CMR Deployment
EJB-QL Question