CodeWeblog.com » bean entity,business logic,component model » EJB knowledge

EJB knowledge

EJB is a server-side sun component model, the usefulness of the largest deployment of distributed applications are similar to Microsoft's. Net technology. With the advantages of cross-platform java, using EJB technology can be deployed distributed systems are not limited to a specific platform.
EJB (Enterprise JavaBean) are part of J2EE, for the development of the definition of a component-based multi-enterprise application standards. Features include Web services support and the core of development tools (SDK).
J2EE in at, Enterprise Java Beans (EJB) Enterprise Java known as Bean, are the core of Java code, are separately conversation Bean (Session Bean), Entity Bean (Entity Bean) and Message-Driven Bean (MessageDriven Bean).
1.Session Bean for the implementation of business logic, it can have are the status, as well as stateless. Whenever the client requests, the container will be a selection for the Session Bean Client Services. Session Bean can directly access the database, but more often, it will achieve through the Entity Bean Data Access.
2.Entity Bean domain model objects are used to achieve O / R mapping, responsible for records in the database table mapped to the Entity object in memory, in fact, create a new Entity Bean object is equivalent to a record, delete an Entity Bean At the same time from the database will delete the corresponding record, modify an Entity Bean, the container will automatically status of the Entity Bean and database synchronization.
EJB2.0 are 3.MessageDriven Bean introduced a new enterprise Bean, which is based on JMS message, the client can only receive the JMS message to send then deal with. MDB is in fact a stateless asynchronous Session Bean, the client having to wait after calling MDB, return immediately, MDB will handle customer requests asynchronous. This treatment is suitable for asynchronous requests necessary occasion, such as order processing, so that the client will be able to avoid a long wait for a return until the results of method calls.
EJB are in fact the SUN in a J2EE specification and provides a series of API used to realize the concept of the EJB into EJB products. EJB are BEANS, BEANS What is the concept that she may have a house so that she Teng Jin to build the place, that is, there is a container. EJB must survive in the EJB container. But the container of the very powerful! her first package you want BEAN, EJB client, and you in fact has never been directly prepared by EJB deal between them through the HOME / REMOTE interface relations happen. BEAN It is responsible for all of you eat or drink in Lhasa sleep, such as the continuing BEAN, security, transaction management ...
1. What is EJB?
A technical specifications: EJB technically not a "product"
EJB is a standard description of the application of building components to be addressed:
Scalability (Scalable)
Distributed (Distributed)
Transaction Processing (Transactional)
Data storage (Persistent)
Security (Secure)
Two. Sun expectations of EJB
Provide a standard distribution, based on the OO component architecture
Mask the complexity of system-level functional requirements
Write once, run anywhere
With non-Java application interoperability between the
Compatible with standard CORBA
Three. Why EJB?
EJB server to complete the "complexity" of the job: the application developers focus on business logic rather than the underlying implementation of the implementation mechanism (similar to 4GL language design goal)
Supporting business
At the same time, a number of operational success or total failure
Can be described in the code outside to deal with the definition of Service Level
Scalability
EJB applications can be based on your growth and expansion
EJB server is often also provides load balancing and
Security: the provision of resources by the EJB server access control
Iv. EJB Architecture
In order to meet the goal of the architecture, describes the specification
Server (Server)
Container (Container)
Class (Class) and an example (Instance)
Home and Remote Interface
The client (Client)
Friday. Simplified programming model
Focus on business logic implementation: EJB is responsible for the life cycle (lifecycle), data storage (persistence), transaction semantics (transactional semantic), safety (security), ...
A common programming model: all kinds of high-level API Service
Java is a programming language
EJB (business logic code) with the express specific business areas (such as banking, retail, etc.) to adapt the logic. It consists of
Business Logic Tier is running in the treatment of the enterprise bean. Enterprise bean from a client to receive data
Deal with it, and send it to the enterprise information system tier for storage; At the same time, it can also access data from memory,
After treatment is sent to the client application.
There are three types of enterprise beans: session beans, entity beans and message-driven beans.
Session bean with the client to describe a brief conversation. When the client after the completion of the implementation, session bean and
Data it will disappear; with a corresponding entity bean are stored in the database are described in the table and his lasting
Solid data, if the client end of the termination or services, the Service will be responsible for the underlying entity bean data storage.
Message-driven bean combines the information session bean and Java Service (JMS) to monitor the function of information, it allowed
Xu a business component to receive JMS message asynchronously.
[Edit this paragraph]
Introduction of EJB3.0
Because of the complexity of EJB2.0, at Spring and Hibernate frameworks, such as the emergence of lightweight, many users turned to in the U.S. call, EJB3.0 specification was released.
EJB3.0 long-awaited specification finally released. In this article, the new norms will be the introduction of a summary, including the addition of meta-data support, EJBQL changes Bean model entity bean access the context of the new method and run-time environment and so on. The author also discussed the EJB in the future to make adjustments, as well as with other EJB3.0 the relationship between the development of norms.
Start
Anyway because of the complexity of EJB in J2EE architecture so that performance has not been very good. EJB Architecture J2EE are perhaps the only one not able to meet its easy to develop and enhance the formation of the productive forces. EJB3.0 specification are trying to make efforts in this regard in order to reduce the complexity of its development. EJB3.0 developers reduce the workload of the bottom of the development, to cancel or minimize it a lot (before implementation of these is necessary) callback method of implementation, and reduces the entity Bean and O / R mapping model complexity.
In this article, I will introduce some of the main EJB3.0 change. It further in-depth understanding of EJB3.0 is very important. Subsequently, I will come from a higher level description has been submitted to the EJB3.0 specification details, and a new model to explain the changes: Entity Bean, O / R mapping model, entity-relationship model and EJB QL (EJB Query Language) and so on.
Background
EJB3.0 in two important changes are: the use of the procedure Java5 Notes Hibernate-based instrument and the O / R mapping model.
Metadata Java5 instrument
Java5 (formerly known as J2SE1.5 or Tiger) to add a new note of instrument procedures. With this tool you can customize the comment tags, through these custom tags to the Notes field, method, type and so on. These comments will not affect the process of semantics, but it is through the tools (compile time or run-time) to interpret these tags and generate additional content (such as deployment descriptor file), or forced some necessary runtime behavior (such as EJB Group pieces of the status characteristics). Annotated source file can resolve analysis (such as compilers or IDE tools) or the use of the APIs in Java5 reflex mechanism. Notes can only be defined at source code level. All because of the draft submitted to the EJB3.0 comment tags in a run-time RetentionPolicy, will increase the types of documents, therefore the storage space occupied, but given instrument manufacturers and container manufacturers convenience.
Hibernate
Hibernate is very popular at present, it is to develop the source code of Java O / R mapping framework, which aims to develop the data from the persistent red tape out of programming. It also has a standard HQL (Hibernate Query Language) language, you can be in the new EJB QL see its shadow. Hibernate in the treatment of data such as query, updates, connection pooling, transaction processing, treatment and other aspects of the relationship between entities is very simple.
Overview
At EJB3.0 specification has been submitted by two of the main changes involved:
1. A set of notes based on the EJB programming model, combined with the adoption of the definition of EJB2.1 deployment descriptors and the definition of some applications interface behavior.
2. New entity Bean model of persistence, EJBQL there are many important changes.
Have some of the above proposals, such as: a new client-side programming model, the use of business interface, as well as the life cycle of entity Bean. Please note that EJB2.1 programming model (including the deployment descriptor and the home / remote interfaces) is still valid. The new simplified model does not completely replace the EJB2.1 model.
EJB Notes
EJB specification Organize an important goal is to reduce the number of original code, and they are perfect for this purpose and give a brief introduction of the approach. At the EJB3.0, any type of enterprise Bean class is just a note add a simple appropriate Java object (POJO). Notes can be used to define bean's business interface, O / R mapping information, resources, reference information, the effect is defined in EJB2.1 deployment descriptors and interfaces are the same. At EJB3.0 deployment descriptors are no longer necessary for the; home interface and no, you do not have to achieve business interface (containers you can achieve these things).
For instance, you can use @ Stateless put comment tags category Java class declaration for a non-status session bean. The session bean is stateful, @ Remove the Notes can be used to tag a particular way, through this note to explain this method in the call after the bean instance will be removed.
Description of components in order to reduce the description of information, norms adopted also by the abnormal configuration (configuration-by-exception) means, which means you are able to provide a footnote to all the specific default values, so most information can be of conventional inference drawn accordingly.
New model of persistent
New entity bean is a simple add a Notes Java Object (POJO). EntityManager visit as soon as it is it has become a persistent object, and become a persistence context (context) part. A persistence context with a transaction context is loosely coupled; strictly speaking, it is implied by a panel session with the co-existence.
The relationship between entities is defined through the notes, O / R mapping is, and to provide several different types of database standards, EJB2.1 at these through the development of staff to design their own models or other techniques to achieve the (such as, self-growth primary key strategy).
In-depth study
Now is the time to learn more about the draft EJB3.0. Let us begin to explore all four types of enterprise-class EJB in the bean, and to see them in the new norms are what.
Stateless session bean
At EJB3.0 specification, write a stateless session bean (SLSB) required only a simple Java class files and layers with Notes @ Stateless it. This bean can be extended javax.ejb.SessionBean interface, but these are not necessary.
SLSB no longer need a home interface, there is no further need for the type of EJB it. Bean interface type can achieve the business may or may not realize it. If you do not realize any business interface, the business will be any public interface methods. If only some business methods will be exposed to the business interface, these methods can be used @ BusinessMethod Notes. By default all interfaces are generated by local (Hong Kong) interface, you can also use the Notes to the statement @ Remote interface for the remote (remote) interface.
The following few lines of code can be defined on a HelloWorldbean the. And at the same bean in EJB2.1 required at least two interfaces, an implementation class and method of implementation of some space, coupled with the deployment descriptor.
import javax.ejb .*;
/ **
* A stateless session bean requesting that a remote business
* Interface be generated for it.
* /
; @ Stateless
; @ Remote
public class HelloWorldBean (
public String sayHello () (
return "Hello World !!!";
)
)
Session bean has state
SFSB except some special note, the session status has bean (SFSB) and like to streamline SLSB:
A SFSB should have a way to initialize itself (in EJB2.1 is through the ejbCreate () to the implementation). At the EJB3.0 specification suggested that these initialization methods can custom finish, and exposed them to put the business interface. Prior to the use of the bean from the client to call the initialization method. Whether or not the current norm organization to provide a note to mark a method for initialization is also in dispute.
Bean provider can use the @ Remove any SFSB Notes to the method of marking to show that this method is called after the bean instance will be removed. Similarly, the norms of organizations are still discussing whether there should be a mechanism to deal with such exceptional circumstances, that is, when this unusual case is an example of bean was removed.
Following the above-mentioned questions are my personal opinion:
Whether or not there should be a note to indicate a method to initialize it? My opinion is - there should be, so that the container can call other methods in at least a way to call to initialize. This is not only unnecessary mistakes can be avoided (because there is no way to call the initialization) and allows the container to determine whether more specific examples can be SFSB reuse. I will put this issue to one place, only the norms of the Organization as a way to provide a note to declare that it is an initialization method.
For the second question is also my opinion yes. This is conducive to the provision of jointly Bean client gain control over it. There is only one problem left behind: that is, once this method call fails, it can remove the bean instance? The answer is no, but it will be at the end of the session are removed.
Message-Driven Bean
Message-Driven Bean is the only interface to be an operational implementation of Bean. Supporting this interface that the bean is the message what kind of system. With regard to the MDB-based JMS, this interface is javax.jms.MessageListener. The attention of MDB operations is not an interface on the true meaning of the business interface, it is only a message interface.
Entity Bean
Entity Bean using the @ Entity to mark the Notes, all entity bean in the property / field notes do not have to use to tag @ Transient. Entity bean's persistent fields through JavaBean-style mechanism, or a statement for public / protected fields to achieve.
Entity bean can use the helper classes to describe their status, but these categories do not have a lasting example of the uniqueness of (persistent identity) characteristics (ie, unique identifier of the field bean, etc.), in fact these categories and their assistants entity bean instances are closely integrated; and these objects or to non-sharing entities to access the object.
Associated entities
Bean between EJB3.0 support two-way one-way linked together, they can be one-on-one, one-to-many, many-to-one or many-to-many association. However, even at the two ends of a two-way link into their own client (owning side) and the other side (inverse side) a different client. Client is responsible for its own circular linked to the database changes. Many-to-many association for their own client's statement must be clear. In fact the other side through isInverse = true for the Notes (which do not have to end their own shows but inferred from the other). It seems the above description, organizations can standardize EJB Say so easy change it?
O / R Mapping
EJB3.0 in the O / R mapping model has also been important changes in it from the original abstract-persistence-schema-based into the current Hibernate-inspired model. Despite the current norm organization but also a discussion on this specific model will be the next version of the draft.
For example, O / R mapping model bean class through the notes to the statement. But also pointed out that this method corresponds to the specific tables and fields. O / R mapping model provides a set of own SQL; and in addition to providing some basic SQL, but also support the development of certain high-level functions. For instance, there is a statement through the @ Column Notes field columnDefinition, you can write this SQL: columnDefinition = "BLOB NOT NULL"
Client model
An EJB client can be an @ Inject Notes "into" the way a bean's business interface references. You can also use another Notes @ javax.ejb.EJBContext.lookup () to achieve the above operation, but did not tell our regulate an ordinary Java client how a Bean instance, because the ordinary are the Java client run in a client container, it can not access the object @ javax.ejb.EJBContex. Now there is another mechanism to achieve the above work is the use of a super-context object: @ javax.ejb.Context (). However, the specification does not indicate how to use the client-side object.
EJB QL
EJB QL can to Notes @ NamedQuery. Note this property has two members are separately name and queryString. Once the definition of the property, it can EntityManager.createNamedQuery (name) to point to this inquiry. You can also create a standard JDBC-style query and use EntityManager.createQuery (ejbqlString) or EntityManager.createNativeQuery (nativeSqlString) (This method is used to perform a local query) to execute the query.
EJB QL has two places of its parameters can be defined. javax.ejb.Query interface provides a definition of parameters to query and update methods of data and so on. Here is an example query EJBQL point:
See Copy to clipboard Print
. ..
; @ NamedQuery (
name = "findAllCustomersWithName",
queryString = "SELECT c FROM Customer c WHERE c.name LIKE: custName"
)
.. ..
; @ Inject public EntityManager em;
customers = em.createNamedQuery ( "findAllCustomersWithName")
. setParameter ( "custName", "Smith")
. listResults ();
Here is a list of some of the EJB QL enhanced features:
Bulk update and delete support.
Direct support within and outside the connection to connect. Pointed out that you run the FETCH JOIN associated entities, Order can specify only a field inquiry.
Can query the results of more than one return value. In fact, you can rely on the return of a class such as the following:
SELECT new CustomerDetails (c.id, c.status, o.count)
FROM Customer c JOIN c.orders o
WHERE o.count> 100 l support group by and having.
Supporting nested where clause subquery.
EJB3.0 submitted at the draft, EJB QL with the standard SQL is very near. Norms in fact even the direct support of the local SQL (just like us, as mentioned above). This may be of some programmers and some not very clear, we will be following a more detailed explanation.
Diversity
Ways permission (Method permissions) and @ MethodPermissions by @ Unchecked Notes to the statement; Similarly, services can also be property @ TransactionAttribute Notes to the statement. Still maintain the norms, resources and environment resources and references cited. These can be the same as the Notes to the statement, but there are some subtle differences. For instance, the context (context) into the environment through the instrument control. Containers in accordance with bean reference to the external environment, an appropriate automatic initialization of the instance variables have already stated. For instance, you can do the following as a data source (DataSource):
; @ Resource (name = "myDataSource") / / Type is inferred from variable
public DataSource customerDB;
In the above example, if you do not specify the name of the resources cited (name) one of the customerDB will then be considered the default value. When all the property can be invoked when the Notes @ Injec can read:
See Copy to clipboard Print
1.; @ Inject public DataSource customerDB;
; @ Inject public DataSource customerDB;
Container is responsible for initialization at run-time instance of the data source customerDB. Deployment of staff must be defined in the container before the good property of these resources.
Even better news is: those who previously would be a thing of the past Detect abnormal. You may declare the application of arbitrary anomalies, rather than thrown out or re-capture CreateException and other similar anomalies such FinderException. Will be packaged in containers out of the system in class javax.ejb.EJBException abnormal only when necessary or when abnormal out IllegalArgumentException or IllegalStateException.
EJB document processing model
Us at the end of this section, let me glance at the rapid EJB container provider treatment patterns at possible changes. Specification is not specific to this position, but I can think of at least two modes.
Approach is the first to use a file to generate EJB deployment model similar to the EJB2.1 documents (including the necessary interfaces and deployment descriptors) then EJB2.1 manner similar to the deployment of the EJB components. Of course, this generated the deployment descriptor is not standard but it probably can be solved with a container for the EJB2.1 and EJB3.0 compatible problem. The following description of this map in the process.
Another method is to take care of a similar deployment of JSP mode. EJB can you put a file in a pre-defined directory, then the EJB container will identify and deal with it, and then you can use to deploy. This method can be set up in the above method based on that, when deployed in support of repeatedly there is a great help. Take into account is also easy to deploy one of the purposes of EJB3.0 specification, I sincerely hope that in the next draft be able to determine when a model (there is at least an informal).
Do you have any ideas?
EJB3.0 formulate norms are orderly, and in order to make EJB development easier, EJB specification Organize efforts are for all to see. Just as they said, all will become easy, but not easy to do this. Now defines the 50 comment tags (some have the next draft will be issued), each with its own default rules and other operations. Of course, I really do not want to EJB3.0 a refurbished version of the EJB2.1 become "EJB 3.0 = EJB 2.1 for dummies" (I hope not to set up the equation). Finally, I still can not help but want to mention some of my own opinion:
First of all, the norm has become really easy to deploy repeatedly, and there is a simple model to access the runtime environment. I do not think it should give up the home interface.
The EJB specification at an early stage, the entity bean used to map a persistent storage. Theory (perhaps only in theory) may need to put an entity bean is mapped to the legacy of the EIS (enterprise information system) system. Consideration for future expansion are so good and can make more use of the business data model entity bean. Also accompanied by the complexity of the entity bean will not be made good. Submitted in this draft, an entity bean is just a mapping database. And are based on non-abstract pattern of persistent and easy data access pattern more easy to develop.
I have reservations about the model changes, I think in the EJB fragment contains SQL script is not a good note. Some developers object to completely contain certain "SQL fragment (SQLness)" (such as @ Table and @ Column Notes). My opinion is that these are good SQLness which we can clearly know what we want in the end what the database. However, it seems to me that some SQL paragraph is not very good, such as columnDefinition = "BLOB NOT NULL", which makes between EJB code and SQL is too closely coupled.
Even though the support of the local SQL looks quite attractive, in fact, embedded in the EJB code in SQL is a very bad idea. Of course, some way to avoid hard-coded in the EJB in SQL, but it should be stated in the specification, while some developers can not be defined in their model.
@ Table assumption is used only for Class Notes. At run-time through the @ Table Notes name of the table the name of the definition of property would have to correspond to an actual database table. This specification should be given clear and consistent description of the model.
Norms also need to more clearly explain the client-side programming model, especially the ordinary java client. Specification assumes that all reference to the use of implied or EJB client. And norms of the client, nor is there a backward compatibility give specific argument.
Transient Notes should be renamed to avoid the transient keyword, and have conflict. In fact, at this point of our departure from the more willing to look a little configuration-by-exception and the definition of the principle of Notes @ Persistent to a specific definition of persistent fields can only @ Persistent Notes Notes are a marker, or it can have some property related to O / R Mapping Notes.
Associated with other norms
EJB3.0 may affect the JSR has JSR175 (java language meta-data tool) and JSR181 (Java Web Services Metadata)
JSR175 and will not have been initially completed and there is too much conflict EJB3.0; JSR181 and EJB3.0 but has two associated places:
Web service interface: EJB specification will be used to adapt to a mechanism so that we can put a JSR181 implementation bean as a Web service and Web service told how to call the client.
JSR 181 plans to use different mechanisms to deal with security issues. In the early EJB specifications recommend the use of a consistent mechanism (MethodPermissions), but JSR 181 plans to use a slightly different way (SecurityRoles and SecurityIdentity Notes). Notes of the same definition of RunAs these many differences also exist. Also solve this problem will eventually J2EE specification layer to maintain its consistency.
At some of the J2EE 1.5 specification development there is probably associated with EJB3.0. Apart from the above mentioned do not have some relevance outside of the development of norms and other conflicts have EJB3.0.
Concluding remarks
EJB development in making simple and efficient, we have a long way to go. EJB specification organizations in the lower aspects of the development of the difficulty of starting a good head. O / R mapping model is still in the early stages of the proposal to standardize the organization is perfect. I hope that it will not be too complex and not to over-coupling and SQL.
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: bean entity (RSS), business logic (RSS), component model (RSS), entity object (RSS), java code (RSS), entity bean (RSS), database table (RSS), javabean (RSS), domain model (RSS), session bean (RSS), enterprise bean (RSS), customer requests (RSS), platform java (RSS), enterprise java beans (RSS), database synchronization (RSS), application standards (RSS), model objects (RSS), access 2 (RSS), new enterprise (RSS), long wait (RSS)

Permalink: http://www.codeweblog.com/ejb-knowledge/

Leave a reply