It is currently Fri May 18, 2012 4:03 pm


Post a new topicPost a reply Page 1 of 1   [ 1 post ]
Author Message
 Post subject: Enterprise Java Beans 3 Tutorial Part 3
PostPosted: Mon Sep 13, 2010 3:06 pm 
Site Admin

Joined: Sat Aug 02, 2008 8:00 am
Posts: 147
Creating the Session Bean
As we mentioned before the session bean is the server side component that contains the business logic for your application. Usually the entity beans are not called directly from the client application they are mostly called through session beans.
To create session beans we will need to create an EJB project. To do that go to the “Project Explorer” view then right click and choose “Project” from the menu. In the window displayed choose EJB project as in the following figure and click “Next”.


Image

In the EJB project window enter “EJB3TutorialSession” as the name of the project and make sure to use JBoss 5 as the targeted runtime and version 3 as the version of the EJB module as in the following figure.

Image


Click “Finish” to start creating the project.
The project should now be created but with no session beans in it.
Next we will create a new session bean in the newly created project. This session bean will create a new Book and save in the database and then it will delete it from the database.
Right click on the project “EJB3TutorialSession” then select “New” then “Session Bean [EJB 3]”.
In the “Create EJB3 Session Bean” window enter the java package as “com.thejavacode.ejb.session” and the class name to be “BooksSession
Check “local” under the “Create Business interface” section in order to create a local interface for your session bean then click “Finish”.


Image



Note: Local Interfaces enables your session beans to be called from the same Java Virtual Machine only
Remote Interfaces Enables your session beans to be called from remote applications outside the Java Virtual Machine containing the session bean.
Eclipse will create the BooksSession session bean in the desired package and it will contan the following code.
package com.thejavacode.ejb.session;

Code:
import javax.ejb.Stateless;

/**
* Session Bean implementation class BooksSession
*/
@Stateless
public class BooksSession implements BooksSessionLocal {

    /**
     * Default constructor.
     */
    public BooksSession() {
        // TODO Auto-generated constructor stub
    }

}

Notice the @Stateless annotation defined before the declaration of the class which marks the class as a session bean for the runtime to treat it as so.
The @Stateless tells the runtime that the following class is a stateless session bean and therefore we do not need to describe it in any deployment descriptor e.g. ejb-jar.xml as in previous versions of EJB.
Notice also that after creating the session bean Eclipse created the local interface for this session bean named BooksSessionLocal in the same package and it looks like this.
Code:
package com.thejavacode.ejb.session;
import javax.ejb.Local;

@Local
public interface BooksSessionLocal {

}

Notice that the interface is annotated with the Local annotation to mark for the target application server that this interface is to be used for local invocation of implemented session beans.
Adding the business Methods to your Session Bean
In this section after we created a new session bean we need to add the method that will be called to perform the desired action for our bean (In this case create and delete a book).
To add the new method we will just add the declaration of the method in the local interface and implement this method in the session bean class.
Here is the local interface after adding the method.

Code:
@Local
public interface BooksSessionLocal {

   public void createAndDeleteBook();
}

And here is the session bean class after adding the implementation
Code:
@Stateless
public class BooksSession implements BooksSessionLocal {

    /**
     * Default constructor.
     */
    public BooksSession() {
        // TODO Auto-generated constructor stub
    }

   @Override
   public void createAndDeleteBook() {
      // TODO Auto-generated method stub
      
   }

}

Notice the @Override annotation which overrides the parent method createAndDeleteBook in the local interface with the method it is annotating.

Declaring the Persistence Context
For the session bean to be able to deal with entity beans on the runtime we need to declare an EntityManager object.
The EntityManager is annotated with the @PersistenceContext annotation which enables it to give the session bean access to the entity beans.
Code:
    @PersistenceContext
   EntityManager em;


Creating the Book bean object and persisting it
After creating the entity manager we will need to create an instance from the Book Entity bean and then set the values on this object and save it in the database as follows

Code:
Book book=new Book();
       book.setCopiesOnShelf(totalnumber);
       book.setTitle(title);
       book.setTotalNumberOfCopies(totalnumber);
       book.setYear(year);
       em.persist(book);

Notice the call to em.persist(book); this is the actual line that will insert the book record in the database. This line shows the power of JPA. Instead of dealing with JDBC API and SQL statements with just one line of code we are able to save the object in the database.

Deleting the book object from the database

To delete the object that we created earlier from the database all the code that it takes is this line.
Code:
em.remove(book);

Again this shows how JPA makes it very simple for developers to perform database operations without the headache of JDBC.

This was the end of this 3 parts serise of EJB 3 Tutorial
I wish you enjoyed it and if you have comments please send it to me.


You do not have the required permissions to view the files attached to this post.

_________________

Muhammad Safwat Fuad
The Java Code Admin
Java Technical Lead.
Mobile: +2010-2942-538
Email:mtv134@yahoo.com


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 1 post ]


Who is online

Registered users: europhory, Illerma, ojrzcnyu


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
twilightBB Style by Daniel St. Jules of Gamexe.net

[
SEO MOD © 2007 StarTrekGuide ]