Introduction to Entity Beans
An “entity bean” is a business encodes persistent object which forms and integral part of JAVA Enterprise edition. This represents a persistent data object from the database. Beans are commonly known and EJB (Entity JAVA Bean). They can persistent independently or can delegate this to the container. It is very resilient and thus was widely used over the distributed network over JAVA EE 6 and before versions. A container is used to host the EJB Services and in case container or server crashes, then also beans survive the crash as these are linked to primary keys present in the database.
These were released as EJB 3.0 and then its superset Java Persistence API (application programming interface) was released. This is now deprecated technology as from JAVA EE 6 version onwards Java Persistence API is used.
Components of Entity Bean
Entity beans form an object view of a primary key stored in the database system. Some examples, if we talk about ERP Database are orders, customer master data, material, etc. Since the objects stored in the database is in a different format than the format data is stored in JAVA beans so to vanish this difference in formats object persistence property is used.
Entity beans link with other entity beans via establishing relationships as they have primary keys assigned. Beans can be broken into some components explained below:
1. Remote component: This interface is responsible to store all the methods pertaining to beans. Generally, multiple business logic are stored with get and set methods to ensure that parameters are properly passed onto the function and retrieved back with proper business logic applied. This information is stored in a bean that is loosely coupled (this means that the bean can be present anywhere over the network and linked with entity bean so that it can be called whenever required).
2. Home interface: The home interface is used to create entity bean methods. These methods can be set, get or find the entity beans which can be located anywhere over the distributed network.
3. Primary key class: This is one of the main classes which differentiate entity beans from session beans. Online session beans which are non-persistent after the session is terminated, entity beans do persist as it contains the primary key class. The primary key is stored in the database as a unique identifier for a table. Due to the presence of a primary key in the database, entity banns can be accessed by multiple clients and do not crash even when the server or container for that bean is crashed. Some functions like hash() and equal() are used for this functionality to be used.
4. Bean class: Bean is a class that links to entity beans and contains business logic which are to be applied on entity beans. These contain life session duration as well if there is a requirement as such.
How the Entity Bean Works?
There are two things involved majorly in entity beans and these are server and client. We need to build client applications separately which will call the server. The server has business logics and other synchronization techniques which consumes the business request and sends out response to the client application. This application can then deliver the information in the desired format to the requester. We need to have a one client application that retrieval logic. The server should have an annotation defined as per its function which may be remote or local.
This can be well understood with the example below:
Examples to Implement Entity Beans
The example for entity bean is provided below with the help of projects created in Eclipse IDE and a server used is wildly 19. We need to ensure some prerequisites to be fulfilled before running any project of JAVA EJB. Some of the prerequisites are Maven installed or not, Javaeee-api-7.0.jar API should be imported in the project. For this case, one jboss-client.jar JAR file and one file of a previous project (MsgServer) should be imported in the MsgClientEJB project for errors to go away. The below screenshots clarify how project directories are placed.
Example #1
Project:
Main1: MsgClientEJB
File 1: ClientApp.java
Code:
package MsgClientEJB;
import javax.naming.*;
import MsgServer.*;
import java.util.*;
public class ClientApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
System.out.println("Client App Started");
Properties props = new Properties();
props.put("java.naming.factory.url.pkgs","org.jboss.ejb.client.naming");
InitialContext context = new InitialContext(props);
String appName = "";
String moduleName = "MsgFromServerEJB";
String distinctName = "";
String beanName = Serverone.class.getSimpleName();
String interfaceName = ServeroneRemote.class.getName();
String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + interfaceName;
System.out.println(name);
ServeroneRemote bean = (ServeroneRemote)context.lookup(name);
String msg = bean.getMsg();
System.out.println(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}
File 2: jobs-ebb-client.properties
Code:
remote.connections=default
remote.connection.default.host=127.0.0.1
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
File3: module-info.java
Code:/**
*
*/
/**
*@authormeghnadwivedi
*
*/
module MsgClientEJB {
requires java.naming;
requires MsgFromServerEJB;
}
Output:
Example #2
Main2: MsGFromServerEJB
File1: Serverone.java
Code:
package MsgServer;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
* Session Bean implementation class ExampleServer. This has stateless attribute as mentioned below. We can have stateful bean as
Well we implemented session bean.
*/
@Stateless
@LocalBean
public class Serverone implements ServeroneRemote, ServeroneLocal {
/**
* Default constructor.
*/
public Serverone() {
//TODO Auto-generated constructor stub
}
@Override
public String getMsg() {
return "This is a message from Server";
}
}
File2: ServeroneLocal.java
Code:
package MsgServer;
import javax.ejb.Local;
// This is local component of the server in JAVA language with an annotation local declared below.
@Local
public interface ServeroneLocal {
public String getMsg();
}
File3: Serveroneremote.java
Code:
package MsgServer;
import javax.ejb.Remote;
// This is remote component of the server in JAVA language with an annotation remote declared below.
@Remote
public interface ServeroneRemote {
public String getMsg();
}
Output:
Conclusion
Entity beans initiated with the easy remote access of JAVA applications where business logic and database entity retrieval logics can be encapsulated in different modules and present at remote locations. This provides a self-driven container or server which takes care of synchronization in this distributed environment to make sure that there are minimum issues die to components present in different locations in different time frames. Although this functionality is now enhanced after JAVA’s upgrade APIs consuming not only remote access facility but also added functionalities. This added persistence API by JAVA covers a lot more interactive features which makes the application more reliant and interactive over the network.
Recommended Articles
This is a guide to Entity Beans. Here we discuss the concept of Entity Beans through definition and their Components along with programming examples and their outputs. You can also go through our other suggested articles to learn more –