File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes why ejbCreate can't be invoked? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "why ejbCreate can Watch "why ejbCreate can New topic

why ejbCreate can't be invoked?

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
i want to do some initial operation when the session bean is first created,but where my initial code should be?i think it should be "ejbCreate()" method,so that i make the following test:
the ejbCreate method of my session bean:

public void ejbCreate() throws CreateException {
System.out.println("this is ejbCreate!");

then i write a test client by using the jb6 test client wizard,in the test client,i invoke the create() method of the home interface of the session bean,i think that the ejbCreate method should be invoked,but it can't,the code snippet as:

Context ctx = new InitialContext();
Object ref = ctx.lookup("session");
sessionHomeObject = (sessionHome) PortableRemoteObject.narrow(ref, sessionHome.class)
session s=sessionHomeObject.create();

when i execute the test client,the string "this is ejbCreate" can't be printed,but i can call any business method of the session bean in the client,i wonder when the ejbCreate method is executed?how can i do some initialization operation,because the session bean has not the constructor.
Viswanatha GB

Joined: Sep 19, 2001
Posts: 16
I don't think so it will not call ejbCreat(),
It will call. noramlly we have to Create the PK ID in the ejbCreat() method is self..
here is the samle code
public String ejbCreate(String productId, String description,
double price) throws CreateException {
if (productId == null) {
throw new CreateException("The productId is required.");
this.productId = productId;
this.description = description;
this.price = price;
return null;
ProductHome .java

public interface ProductHome extends EJBHome {
public Product create(String productId, String description,
double balance) throws RemoteException, CreateException;

public Product findByPrimaryKey(String productId)
throws FinderException, RemoteException;

public Collection findByDescription(String description)
throws FinderException, RemoteException;
public Collection findInRange(double low, double high)
throws FinderException, RemoteException;
public class ProductClient {
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("jdbc/Cloudscape");
ProductHome home =
Product duke = home.create("123", "Ceramic Dog", 10.00);

I think it helps you..but i'm sure it will call ejbCreat methoid..check out..
[ April 04, 2002: Message edited by: viswagb ]

==============(**)=================<br />Viswanatha.G.B<br />Sr.Software Engineer<br />IBM<br />==============(**)=================
I agree. Here's the link:
subject: why ejbCreate can't be invoked?
It's not a secret anymore!