• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Could not create entity com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using JBoss 4.2, mysql 5 and EJB 2.x I receive the following error Could not create entity
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'product_id' cannot be null. I cannot see where I am going wrong? What is making the product_id null and how can I correct this?

Thanks in advance


Test Client
package com.product.test;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;

/**
* The Class TestClient.
*/
public class TestClient {

/**
* Gets the initial context.
*
* @return the initial context
*
* @throws NamingException
* the naming exception
*/
public static Context getInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
p.setProperty("java.naming.provider.url", "localhost");
return new javax.naming.InitialContext(p);
}

/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
try
{
Context jndiContext = getInitialContext();
Object obj=jndiContext.lookup("ProductManagerHomeRemote");
com.javasrc.licenseserver.ejb.productmanager.ProductManagerHomeRemote home = (com.javasrc.licenseserver.ejb.productmanager.ProductManagerHomeRemote)
javax.rmi.PortableRemoteObject.narrow(obj,com.javasrc.licenseserver.ejb.productmanager.ProductManagerHomeRemote.class);
com.javasrc.licenseserver.ejb.productmanager.ProductManagerRemote productManager = home.create();
productManager.addKeyToProduct( "My Product 1", new Integer( 1 ), "My Key" );
/*
* productManager.addProduct( new Integer( 1 ), "My Product 1" );
* productManager.addProduct( new Integer( 2 ), "My Product 2" );
* productManager.addProduct( new Integer( 3 ), "My Product 3" );
* productManager.removeProduct( "My Product 1" );
* productManager.removeProduct( "My Product 2" );
* productManager.removeProduct( "My Product 3" );
* productManager.addProduct( new Integer( 1 ), "My Product 1" );
* productManager.addProduct( new Integer( 2 ), "My Product 2" );
* productManager.addProduct( new Integer( 3 ), "My Product 3" );
*/
}
catch( Exception e )
{
e.printStackTrace();
}
}
}


ejb-jar.xml
<!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>
<enterprise-beans>
<entity>
<ejb-name>ProductEJB</ejb-name>
<home>
com.javasrc.licenseserver.ejb.product.ProductHomeRemote
</home>
<remote>
com.javasrc.licenseserver.ejb.product.ProductRemote
</remote>
<local-home>
com.javasrc.licenseserver.ejb.product.ProductHomeLocal
</local-home>
<local>
com.javasrc.licenseserver.ejb.product.ProductLocal
</local>
<ejb-class>
com.javasrc.licenseserver.ejb.product.ProductBean
</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>Product</abstract-schema-name>
<cmp-field>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<primkey-field>id</primkey-field>
<security-identity>
<use-caller-identity />
</security-identity>
<query>
<query-method>
<method-name>findByName</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
SELECT OBJECT(p) FROM Product p WHERE p.name = ?1
</ejb-ql>
</query>
</entity>
<entity>
<ejb-name>ProductKeyEJB</ejb-name>
<home>
com.javasrc.licenseserver.ejb.productkey.ProductKeyHomeRemote
</home>
<remote>
com.javasrc.licenseserver.ejb.productkey.ProductKeyRemote
</remote>
<local-home>
com.javasrc.licenseserver.ejb.productkey.ProductKeyHomeLocal
</local-home>
<local>
com.javasrc.licenseserver.ejb.productkey.ProductKeyLocal
</local>
<ejb-class>
com.javasrc.licenseserver.ejb.productkey.ProductKeyBean
</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>ProductKey</abstract-schema-name>
<cmp-field>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<field-name>key</field-name>
</cmp-field>
<primkey-field>id</primkey-field>
<security-identity>
<use-caller-identity />
</security-identity>
</entity>
<session>
<ejb-name>ProductManagerEJB</ejb-name>
<home>
com.javasrc.licenseserver.ejb.productmanager.ProductManagerHomeRemote
</home>
<remote>
com.javasrc.licenseserver.ejb.productmanager.ProductManagerRemote
</remote>
<ejb-class>
com.javasrc.licenseserver.ejb.productmanager.ProductManagerBean
</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-local-ref>
<ejb-ref-name>ProductHomeLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>
com.javasrc.licenseserver.ejb.product.ProductHomeLocal
</local-home>
<local>
com.javasrc.licenseserver.ejb.product.ProductHomeLocal
</local>
<!-- ejb-link is required by jboss for local-refs. -->
<ejb-link>ProductEJB</ejb-link>
</ejb-local-ref>
</session>
</enterprise-beans>

<relationships>
<ejb-relation>
<ejb-relation-name>Product-ProductKey</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
Product-has-many-ProductKeys
</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>ProductEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>productKeys</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
ProductKey-belongs-to-Product
</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete />
<relationship-role-source>
<ejb-name>ProductKeyEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>product</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>

</relationships>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>ProductEJB</ejb-name>
<method-name>*</method-name>
</method>
<method>
<ejb-name>ProductKeyEJB</ejb-name>
<method-name>*</method-name>
</method>
<method>
<ejb-name>ProductManagerEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

</ejb-jar>

jbosscmp-jdbc.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE jbosscmp-jdbc PUBLIC
"-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN"
"http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_0.dtd">

<jbosscmp-jdbc>

<defaults>
<datasource>java:/LicenseServerDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>false</create-table>
<remove-table>false</remove-table>
</defaults>

<enterprise-beans>
<entity>
<ejb-name>ProductEJB</ejb-name>
<table-name>Product</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>product_id</column-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
<column-name>product_name</column-name>
</cmp-field>
</entity>
<entity>
<ejb-name>ProductKeyEJB</ejb-name>
<table-name>ProductKey</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>pk_id</column-name>
</cmp-field>
<cmp-field>
<field-name>key</field-name>
<column-name>pk_key</column-name>
</cmp-field>
</entity>
</enterprise-beans>

<relationships>
<ejb-relation>
<ejb-relation-name>Product-ProductKey</ejb-relation-name>
<foreign-key-mapping />
<ejb-relationship-role>
<ejb-relationship-role-name>
Product-has-many-ProductKeys
</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>id</field-name>
<column-name>product_id</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
ProductKey-belongs-to-Product
</ejb-relationship-role-name>
<key-fields />
</ejb-relationship-role>
</ejb-relation>
</relationships>

</jbosscmp-jdbc>

jboss.xml
<?xml version="1.0"?>

<jboss>
<enterprise-beans>
<entity>
<ejb-name>ProductEJB</ejb-name>
<local-jndi-name>ProductHomeLocal</local-jndi-name>
</entity>
<entity>
<ejb-name>ProductKeyEJB</ejb-name>
<local-jndi-name>ProductKeyHomeLocal</local-jndi-name>
</entity>
<session>
<ejb-name>ProductManagerEJB</ejb-name>
<jndi-name>ProductManagerHomeRemote</jndi-name>
</session>
</enterprise-beans>
</jboss>


ProductBean
package com.javasrc.licenseserver.ejb.product;

import java.util.Set;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.naming.InitialContext;

import com.javasrc.licenseserver.ejb.productkey.ProductKeyHomeLocal;
import com.javasrc.licenseserver.ejb.productkey.ProductKeyLocal;

public abstract class ProductBean implements EntityBean
{
/**
* Adds the specified key to this product
*/
public void addProductKey( Integer id, String key ) throws EJBException
{
try
{
InitialContext jndiEnc = new InitialContext();
ProductKeyHomeLocal productKeyHome = ( ProductKeyHomeLocal )jndiEnc.lookup( "ProductKeyHomeLocal" );
ProductKeyLocal productKey = productKeyHome.create( id, key );

Set productKeys = getProductKeys();
productKeys.add( productKey );
}
catch( Exception e )
{
throw new EJBException( e );
}
}

public void ejbActivate() {}

public Integer ejbCreate( Integer id, String name ) throws CreateException
{
setId( id );
setName( name );
return null;
}

public void ejbLoad() {}
public void ejbPassivate() {}

public void ejbPostCreate( Integer id, String name )
{
}
public void ejbRemove() {}

public void ejbStore() {}
public abstract Integer getId();

public abstract String getName();
public abstract Set getProductKeys();
public void setEntityContext( EntityContext ctx ) {}
public abstract void setId( Integer id );
public abstract void setName( String name );
public abstract void setProductKeys( Set productKeys );
public void unsetEntityContext() {}
}

ProductManagerBean
package com.javasrc.licenseserver.ejb.productmanager;

import java.rmi.RemoteException;

import javax.ejb.SessionContext;
import javax.naming.InitialContext;

import com.javasrc.licenseserver.ejb.product.ProductHomeLocal;
import com.javasrc.licenseserver.ejb.product.ProductLocal;

public class ProductManagerBean implements javax.ejb.SessionBean
{
/**
*
*/
private static final long serialVersionUID = 3412364976175301892L;
public SessionContext context;
private InitialContext ic = null;

public void addKeyToProduct( String productName, Integer keyId, String key ) throws RemoteException
{
try
{
// Get our Product Home
InitialContext jndiContext = getInitialContext();
Object obj = jndiContext.lookup( "ProductHomeLocal" );
ProductHomeLocal productHome = ( ProductHomeLocal )obj;

// Find our product
ProductLocal product = productHome.findByName( productName );
product.addProductKey( keyId, key );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void addProduct( Integer id, String productName ) throws RemoteException
{
try
{
// Get our Product Home
InitialContext jndiContext = getInitialContext();
Object obj = jndiContext.lookup( "ProductHomeLocal" );
ProductHomeLocal productHome = ( ProductHomeLocal )obj;

// Create our product
ProductLocal product = productHome.create( id, productName );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void ejbActivate()
{
}
public void ejbCreate()
{
}
public void ejbPassivate()
{
}

public void ejbRemove()
{
}

public InitialContext getInitialContext() throws javax.naming.NamingException
{
if( ic == null )
{
ic = new InitialContext();
}
return ic;
}

public void removeProduct( String productName ) throws RemoteException
{
try
{
// Get our Product Home
InitialContext jndiContext = getInitialContext();
Object obj = jndiContext.lookup( "ProductHomeLocal" );
ProductHomeLocal productHome = ( ProductHomeLocal )obj;

// Find our product
ProductLocal product = productHome.findByName( productName );
product.remove();
}
catch( Exception e )
{
e.printStackTrace();
}
}

public void setSessionContext(SessionContext ctx)
{
context = ctx;
}

}

ProductKeyBean
package com.javasrc.licenseserver.ejb.productkey;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;

import com.javasrc.licenseserver.ejb.product.ProductLocal;

public abstract class ProductKeyBean implements EntityBean
{
public void ejbActivate() {}

public Integer ejbCreate( Integer id, String key ) throws CreateException
{
setId( id );
setKey( key );
return null;
}

public void ejbLoad() {}
public void ejbPassivate() {}

public void ejbPostCreate( Integer id, String key )
{
}
public void ejbRemove() {}

public void ejbStore() {}
public abstract Integer getId();

public abstract String getKey();
public abstract ProductLocal getProduct();
public void setEntityContext( EntityContext ctx ) {}
public abstract void setId( Integer id );
public abstract void setKey( String key );
public abstract void setProduct( ProductLocal product );
public void unsetEntityContext() {}
}
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic