This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

why ejbCreate can't be invoked?

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
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
Greenhorn

Joined: Sep 19, 2001
Posts: 16
Hi,
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
ProductEJB.java
=========================================
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;
}
=======================================
Client
=======================================
public class ProductClient {
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("jdbc/Cloudscape");
ProductHome home =
(ProductHome)PortableRemoteObject.narrow(objref,
ProductHome.class);
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..
viswa
[ April 04, 2002: Message edited by: viswagb ]

==============(**)=================<br />Viswanatha.G.B<br />Sr.Software Engineer<br />IBM<br />==============(**)=================
 
GeeCON Prague 2014
 
subject: why ejbCreate can't be invoked?