- This topic has 9 replies, 3 voices, and was last updated 20 years, 3 months ago by Greg.
-
AuthorPosts
-
hillelMemberWhen I add the following to SimpleBeanUtil:
*
* @ejb.create-method
*/
public String ejbCreate() throws CreateException {
this.setId(SimpleBeanUtil.generateGUID(this));
return null;
}I get two errors
1)You need to import javax.ejb.CreateException as CreateException is undefined
2)The Method setId(String) is undefined for SimpleBeanUtilThe method public abstract void setId(String id) should have been carried from the SimpleBean and the import as well.
This should not happen. Any ideas why, as I’ve followed each point as in the tut
GregMemberCould you post all of your EJB source files? Maybe we could help better if we see the whole picture.
hillelMember/*
* Created on Aug 2, 2004
*
* TODO To change the template for this generated file go to
* Window – Preferences – Java – Code Style – Code Templates
*/
package de.laliluna.tutorial.simpleBean.ejb;import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;/**
* @author hillel
*
* TODO To change the template for this generated type comment go to
* Window – Preferences – Java – Code Style – Code Templates
*/
/**
* XDoclet-based CMP entity bean. This class must be declared
* <code>public abstract</code> because the concrete class will
* be implemented by the CMP provider’s tooling.<br>
*
* To generate code:
* <br>
* <ul>
* <li> Add Standard EJB module to XDoclet project properties
* <li> Customize XDoclet configuration
* <li> Run XDoclet
* </ul>
* <br>
* Please see the included XDoclet Overview
* and the XDoclet Reference in the help system for details
*
* @ejb.bean name = “SimpleBean”
* type = “CMP”
* cmp-version = “2.x”
* display-name = “SimpleBean”
* description = “SimpleBean EJB”
* view-type = “both”
* jndi-name = “ejb/SimpleBeanHome”
* local-jndi-name = “ejb/SimpleBeanLocalHome”
* primkey-field = “id”
* @ejb.persistence table-name = “tsimplebean”
* @jboss.persistence table-name = “tsimplebean”
* @ejb:util
* generate=”physical”
*/
//Now we will add the Primary key field id and a second
//field namepublic abstract class SimpleBean implements EntityBean {
/** The EntityContext */
private EntityContext context;/**
* @ejb.interface-method view-type = “both”
* @ejb.persistence column-name = “fid”
* @ejb.pk-field
*
* @return
*/
public abstract String getId();/**
* @ejb.interface-method view-type = “both”
*
* @param name
*/
public abstract void setId(String id);/**
* @ejb.interface-method view-type = “both”
* @ejb.persistence column-name = “fname”
*
* @return
*/
public abstract String getName();/**
* @ejb.interface-method view-type = “both”
*
* @param name
*/
public abstract void setName(String name);
public String ejbCreate() throws CreateException {
return null;
}/**
* For each ejbCreate<METHOD>(…) method, there is a matching ejbPostCreate<
* METHOD>(…) method that has the same input parameters but whose return type is
* void. The container invokes the matching ejbPostCreate<METHOD>(…) method on
* an instance after it invokes the ejbCreate<METHOD>(…) method with the same arguments.
* The instance can discover the primary key by calling getPrimaryKey() on its
* entity context object. <br>
*
* The entity object identity is available during the ejbPostCreate<METHOD>(…)
* method. The instance may, for example, obtain the component interface of the associated entity
* object and pass it to another enterprise bean as a method argument.<br>
*
* The entity Bean Provider may use the ejbPostCreate<METHOD>(…) to set the values
* of cmr-fields to complete the initialization of the entity bean instance.
* An ejbPostCreate<METHOD>(…) method executes in the same transaction context as
* the previous ejbCreate<METHOD>(…) method.
*
* @throws CreateException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbPostCreate() throws CreateException {
}/**
* The container invokes this method on the instance when the container picks the instance from
* the pool and assigns it to a specific entity object identity. The ejbActivate() method gives
* the entity bean instance the chance to acquire additional resources that it needs while it is in the
* ready state.<br>
*
* This method executes with an unspecified transaction context. The entity bean must not
* attempt to access its persistent state or relationships using the accessor methods during this
* method.<br>
*
* The instance can obtain the identity of the entity object via the getPrimaryKey(), getEJBLocalObject(),
* or getEJBObject() method on the entity context. The instance
* can rely on the fact that the primary key and entity object identity will remain associated with
* the instance until the completion of ejbPassivate() or ejbRemove().
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbActivate() throws EJBException {
}/**
* The container invokes this method on an instance when the container decides to disassociate
* the instance from an entity object identity, and to put the instance back into the pool of available
* instances. The ejbPassivate() method gives the instance the chance to release any
* resources that should not be held while the instance is in the pool. (These resources typically
* had been allocated during the ejbActivate() method.) <br>
*
* This method executes with an unspecified transaction context. The entity bean must not
* attempt to access its persistent state or relationships using the accessor methods during this
* method.<br>
*
* The instance can still obtain the identity of the entity object via the getPrimaryKey(),
* getEJBLocalObject(), or getEJBObject() method of the EntityContext interface.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbPassivate() throws EJBException {
}/**
* When the container needs to synchronize the state of an enterprise bean instance with the
* entity object�s persistent state, the container calls the ejbLoad() method.<br>
*
* The entity Bean Provider can assume that the instance�s persistent state has been loaded just
* before the ejbLoad() method is invoked. It is the responsibility of the Bean Provider to use
* the ejbLoad() method to recompute or initialize the values of any instance variables that
* depend on the entity bean�s persistent state. In general, any transient state that depends on the
* persistent state of an entity bean should be recalculated using the ejbLoad() method. The
* entity bean can use the ejbLoad() method, for instance, to perform some computation on
* the values returned by the accessor methods (for example, uncompressing text fields).<br>
*
* This method executes in the transaction context determined by the transaction attribute of the
* business method that triggered the ejbLoad method. <br>
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbLoad() throws EJBException {
}/**
* When the container needs to synchronize the state of the entity object�s persistent state with the
* state of the enterprise bean instance, the container first calls the ejbStore() method on the
* instance.<br>
*
* The entity Bean Provider should use the ejbStore() method to update the instance using
* the accessor methods before its persistent state is synchronized. For example, the ejb-
* Store() method may perform compression of text before the text is stored in the database.
* The Bean Provider can assume that after the ejbStore() method returns, the persistent state
* of the instance is synchronized.<br>
*
* This method executes in the same transaction context as the previous ejbLoad or ejbCreate
* method invoked on the instance. All business methods invoked between the previous
* ejbLoad or ejbCreate<METHOD> method and this ejbStore method are also invoked
* in the same transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void ejbStore() throws EJBException {
}/**
* The container invokes the ejbRemove() method on an entity bean instance in response to a
* client-invoked remove operation on the entity bean�s home or component interface or as the
* result of a cascade-delete operation. The instance is in the ready state when ejbRemove() is
* invoked and it will be entered into the pool when the method completes.<br>
*
* The entity bean provider can use the ejbRemove method to implement any actions that must
* be done before the entity object�s persistent representation is removed.
* The container synchronizes the instance�s state before it invokes the ejbRemove method.
* This means that the state of the instance at the beginning of the ejbRemove method is the
* same as it would be at the beginning of a business method.
* This method and the database delete operation(s) execute in the transaction context determined
* by the transaction attribute of the remove method that triggered the ejbRemove method.
* The instance can still obtain the identity of the entity object via the getPrimaryKey(),
* getEJBLocalObject(), or getEJBObject() method of the EntityContext interface.<br>
*
* After the entity Bean Provider�s ejbRemove returns, and in the same transaction context, the
* Container removes the entity bean from all relationships in which it participates before removing
* the entity object�s persistent representation. <br>
* Since the instance will be entered into the pool, the state of the instance at the end of this
* method must be equivalent to the state of a passivated instance. This means that the instance
* must release any resource that it would normally release in the ejbPassivate() method.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*
* @throws RemoveException Thrown if the enterprise bean does not allow destruction of the object.
*/
public void ejbRemove() throws EJBException, RemoveException {
}/**
* Set the associated entity context. The container calls this method
* after the instance creation. The entity bean must not attempt to
* access its persistent state and relationships using the accessor
* methods during this method. <br>
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void setEntityContext(EntityContext newContext) throws EJBException {
context = newContext;
}/**
* Unset the associated entity context. A container invokes this method
* before terminating the life of the instance. The entity bean must not
* attempt to access its persistent state and relationships using the
* accessor methods during this method. <br>
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if the instance could not perform
* the function requested by the container because of an system-level error.
*/
public void unsetEntityContext() throws EJBException {
context = null;
}
}A problem I’ve seen is that the files the xdoclet creates are:
SimpleCMP.java, Simple.java,SimpleData.java,SimpleHome.java,SimpleLocal.java,
SimpleLocalHome.java,SimplePK.java,SimpleUtil.java
Should they now have Bean in the name.Another question is should the datasource in the jboss Xdoclet be java:/MyDS
or java:/simpleBean as the picture and the text describing the datasource seem
to be different.Thanks for your help
GregMemberhillel,
One thing about your first post, you don’t need to add a ejbCreate() method to the Util class. The ejbCreate() method goes in your source Entity bean file. That Util class is also autogenerated by xdoclet so your changes would be lost the next time you ran XDoclet.
I was able to sucessfully deploy your posted Entity bean source file using the “Standard EJB” configuration. But I had to add a “jboss” subtask to the “ejbdoclet” in the MyEclipse-XDoclet property page. Also, if you use a String as your primary key type, xdoclet will generate a primary key class for CMP beans. In your case, SimplePK.java. So you need to change your ejbCreate() method in the SimpleBean class to return type SimplePK.
Also, the generated files follow the standard EJB pattern, which is if you have a bean that starts ends with Bean, then whatever preceeds it will be used as the template for the rest of the generated files.
Hope this helps.
hillelMemberGreg,
Do you mean I should add as the tutorial says with return SimplePK:
public String ejbCreate() throws CreateException {
this.setId(SimpleUtil.generateGUID(this));
return SimplePK;
}However, Myeclipse will not accept the return SimplePK and shows it in red as a error.
I’ve even included:
import de.laliluna.tutorial.simpleBean.interfaces.SimpleUtil;
import de.laliluna.tutorial.simpleBean.interfaces.SimplePK;which enables me to use SimpleUtil.generateGUID(this), but the SimplePK I don’t know how to return it, so Eclipse accepts it.
How diid you get it to work?
Thanks
GregMemberSorry for the confusion. You had the “return null” correct but you need to modify the method signature to return the type SimplePK.
public SimplePK ejbCreate() throws CreateException { this.setId(SimpleUtil.generateGUID(this)); return null; }
hillelMemberGreg,
I’m almost there as now I can deploy the jar, but the client is throwing an exception:
Global JNDI Namespace
+- HAILConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
+- jmx (class: org.jnp.interfaces.NamingContext)
| +- invoker (class: org.jnp.interfaces.NamingContext)
| | +- RMIAdaptor (proxy: $Proxy20 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)
| +- rmi (class: org.jnp.interfaces.NamingContext)
| | +- RMIAdaptor[link -> jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)
+- SimpleLocal (proxy: $Proxy42 implements interface de.laliluna.tutorial.simpleBean.interfaces.SimpleLocalHome)
+- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
+- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
+- UserTransactionSessionFactory (proxy: $Proxy12 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
+- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
+- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
+- invokers (class: org.jnp.interfaces.NamingContext)
| +- 0.0.0.0 (class: org.jnp.interfaces.NamingContext)
| | +- pooled (class: org.jboss.invocation.pooled.interfaces.PooledInvokerProxy)
| +- mobilegw (class: org.jnp.interfaces.NamingContext)
| | +- http (class: org.jboss.invocation.http.interfaces.HttpInvokerProxy)
+- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
+- UILXAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
+- HAILXAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
+- Simple (proxy: $Proxy45 implements interface de.laliluna.tutorial.simpleBean.interfaces.SimpleHome,interface javax.ejb.Handle)
+- UIL2XAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
+- queue (class: org.jnp.interfaces.NamingContext)
| +- A (class: org.jboss.mq.SpyQueue)
| +- testQueue (class: org.jboss.mq.SpyQueue)
| +- ex (class: org.jboss.mq.SpyQueue)
| +- DLQ (class: org.jboss.mq.SpyQueue)
| +- D (class: org.jboss.mq.SpyQueue)
| +- C (class: org.jboss.mq.SpyQueue)
| +- B (class: org.jboss.mq.SpyQueue)
+- topic (class: org.jnp.interfaces.NamingContext)
| +- testDurableTopic (class: org.jboss.mq.SpyTopic)
| +- testTopic (class: org.jboss.mq.SpyTopic)
| +- securedTopic (class: org.jboss.mq.SpyTopic)
+- console (class: org.jnp.interfaces.NamingContext)
| +- PluginManager (proxy: $Proxy21 implements interface org.jboss.console.manager.PluginManagerMBean)
+- UIL2ConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
+- UILConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
+- UUIDKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory)package TestSimpleEJB;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import de.laliluna.tutorial.simpleBean.interfaces.Simple;
import de.laliluna.tutorial.simpleBean.interfaces.SimpleHome;/**
* @author HS
* 24.06.2004
*/
public class TestSimpleBean {public static void main(String[] args) {
try {
Context context = new InitialContext();
Object object = context.lookup(SimpleHome.JNDI_NAME);
SimpleHome simpleHome = (SimpleHome)PortableRemoteObject.narrow(object,SimpleHome.class);
// use the interfaces not the class itself !!!!
Simple simple = simpleHome.create();
simple.setName(“Karl Heinz”);
System.out.println(simple.getPrimaryKey());
System.out.println(simple.getName());
}
catch (NamingException e) {
e.printStackTrace();
}
catch (RemoteException e) {
e.printStackTrace();
}
catch (CreateException e) {
e.printStackTrace();
}}
}When I run Eclipse I get:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at TestSimpleEJB.TestSimpleBean.main(TestSimpleBean.java:21)This is strange as my SimpleHome is:
/*
* Generated by XDoclet – Do not edit!
*/
package de.laliluna.tutorial.simpleBean.interfaces;/**
* Home interface for Simple.
* @xdoclet-generated at ${TODAY}
* @copyright The XDoclet Team
* @author XDoclet
* @version ${version}
*/
public interface SimpleHome
extends javax.ejb.EJBHome
{
public static final String COMP_NAME=”java:comp/env/ejb/Simple”;
public static final String JNDI_NAME=”Simple”;public de.laliluna.tutorial.simpleBean.interfaces.Simple create()
throws javax.ejb.CreateException,java.rmi.RemoteException;public de.laliluna.tutorial.simpleBean.interfaces.Simple findByPrimaryKey(de.laliluna.tutorial.simpleBean.interfaces.SimplePK pk)
throws javax.ejb.FinderException,java.rmi.RemoteException;}
Any help appreciated..
GregMemberIf you write a ejb client that runs in the jboss appserver (like a servlet) you don’t have to do anything extra. However, if you run a test client from the eclipse workbench you have to provide a couple of java properties. One way to do this is to add a jndi.properties file to the same package as your test source file. Put the following lines in your jndi.properties file.
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=localhost:1099
I haven’t tested this but I do remember you have to do this to test a local client. If you don’t want to use the file, you can manually add these two properties in your java source code, but I find using the jndi.properties file easier.
worthcMemberI am recieving the following error wihile trying to use MyEclipse to test an EJB for JBoss.
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at com.convert.test.TestSimpleBean.main(TestSimpleBean.java:29)😕
GregMemberDo you have a jndi.properites file in your classpath of your test client? You can put the jndi.properties file at the same location as your ejbclient test class.
-
AuthorPosts