File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes BMP Entity EJBs and create() 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 "BMP Entity EJBs and create()" Watch "BMP Entity EJBs and create()" New topic
Author

BMP Entity EJBs and create()

Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
I was reading jGuru's page on What is a BMP bean?. I think it's finally clicked for how I need to use Entity EJBs with the proprietary API that I unfortunately have to work with.
One remaining question that I have from that jGuru page is what happens with the create() method in the Home Interface.
I know that in a CMP, the container writes the code needed to create a new entity with a primary key. However, since I am using BMP and need to have my own data access logic handle this, how would I accomplish it? The jGuru article leaves that detail out.
Shoaib Khanzada
Greenhorn

Joined: Mar 25, 2003
Posts: 10
suppose it is ur callback method for creating customer instances.
But dont forget to create connection first. I hope it will be a good example for u. This time i am in hurry if u still find any problem post it here i will reply (inshaallah).
public String ejbCreate(String id, String name,
String address, String telephone){
try{
System.out.println("in ejbCreate()");
addCustomer(id, name, address, telephone);
}
catch(Exception e){
throw new EJBException(e.getMessage());
}
this.id = id;
this.name = name;
this.address = address;
this.telephone = telephone;
return id;
}
////////////////////DATABASE/////////////////////
public void addCustomer(String id, String name,
String address, String telephone)
throws SQLException{
String query = "INSERT INTO CUSTOMER VALUES(?, ?, ?, ?)";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, address);
ps.setString(4, telephone);
ps.executeUpdate();
ps.close();
}
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi,
The process which runs at the background on calling create() method of home interface are as follows:
In the case of entity beans already bean instances would be created and will be in the pooled state.
When u invoke the create() method of the home interface,an EJBObject would be generated by the EJBServer and an instance is taken from the pool and assigned to this EJBObject.Then that instance's ejbCreate() would be called wherein u write ur own code to insert entries to DataBase.
Finally it returns a primary key to the container and the primary key is embedded in the ejbObject and this object(Remote stub) is return to the client thru which client can access the business methods of the bean.
Cheers
Geeta
Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
Originally posted by Shoaib Khanzada:
public String ejbCreate(String id, String name,
String address, String telephone){
try{
<snip>
}
}

Thanks, Shoaib. Would I put that code in the implementation bean (CustomerBean.java) or in the home interface (CustomerHome.java)?
I assume it would go in the bean class.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1132

Hi Michael,
Perhaps this thread is relevant to you?
http://www.coderanch.com/t/311386/EJB-JEE/java/Some-basic-questions
Hope it helps.
Good Luck,
Avi.
Shoaib Khanzada
Greenhorn

Joined: Mar 25, 2003
Posts: 10
yes it is the code for CustomerBean class. How u expect that it will come in CustomerHome dont u know the convention. Here i am giving u the convention always make beans to follow these conventions it will help u to manage ur code.
Home Interface = CustomerHome (Bean name follwed by Home)
Remote Interface = Customer (Just bean name)
Bean Class = CustomerBean (Bean name followed by Bean)
Client = CustomerClient (Bean name followed by Client)
I hope now u will get the idea so that is code and is implemented by ur bean class and this time the bean class is 'CustomerBean'
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BMP Entity EJBs and create()