Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

"Online Tweeter Enterprise Application" : Creating a Stateless Session Bean in NetBeans EJB module - Part 5


Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Stateless Session Bean by name "TweetsFinder.java". A Stateless Session Bean is a enterprise bean that is short - lived object which fulfill single client request and remember nothing about the client in subsequent requests.

Step 1: Open "tweeter-ejb" project and right click Source Packages and then select New and than Other as shown in fig below:










Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Enterprise JavaBeans and in the File Types: select Session Bean as shown in fig below.































Step 3: Click

.
New Session Bean dialog box gets open. It prompts us to enter EJB Name: , Project: , Location: , Package: , Session Type: and Create Interface: etc. Enter the values as shown in the fig below.

Step 4: Click




 

A new Stateless Session Bean gets created by name "TweetsFinder.java" in the "tweeter-ejb" module in the package provided at the time of creating the bean. It has most of the source code already generated by NetBeans. Kindly add or remove additional code provided below.


package com.hubberspot.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

// @Stateless annotation provides container information to
// treat this Java class as a Stateless Session Bean.
@Stateless
public class TweetsFinder {

    // @PersistenceContext injects dependency for the EntityManager
    // by loading persistence.xml
    // EntityManager provides an interface for database persistence
    // such as to persist, merge , load and query objects.
    @PersistenceContext(name = "Tweeter-ejbPU")
    EntityManager tweetsManager;

    // This Stateless Session Bean has only one method
    // findAllTweets(). 
    public List< Tweet > findAllTweets() {

        // Here, it creates CriteriaQuery object
        // from EntityManager by calling its 
        // getCriteriaBuilder().createQuery() which returns 
        // back instance of CriteriaQuery.
        CriteriaQuery criteriaQuery = tweetsManager.getCriteriaBuilder().createQuery();

        // CriteriaQuery has two methods by name select()
        // and from which fetches data from database as 
        // normal SQL select query.        
        criteriaQuery.select(criteriaQuery.from(Tweet.class));

        // Creates a Query instance using criteriaQuery object
        // and EntityManager's createQuery() method.    
        Query query = tweetsManager.createQuery(criteriaQuery);

        // Finally it returns back list of tweets stored in 
        // database.    
        return query.getResultList();

    }
}





Video tutorial to demonstrate How to create a Java EE Stateless Session Bean (EJB) in an Enterprise Application using NetBeans.








In the next section of this blog (part 6) you will learn how to create a JSP page in NetBeans for this application in the Web module.



 
© 2021 Learn Java by Examples Template by Hubberspot