Adopting the Java persistence framework: selection, timing, and advantages and disadvantages?

Source: Internet
Author: User
Tags glassfish

For developing new software systems, object-oriented programming is undoubtedly the most widely used programming model today. Due to the persistence requirements of commercial data, relational database management system (RDBMS) has been widely used. RDBMS uses a relational model, which is different from the domain object model in the software system. When developing software systems using object-oriented programming languages and using RDBMS to persistently store data, the data persistence framework will become a key and important component in the application architecture, they are used to hide the underlying complexity of application data persistence.

In the past few years, some Persistence frameworks have been well developed to help you manage object link ing and data persistence requirements. However, selecting a suitable framework as needed is not a simple task, because multiple factors will affect this decision. In this article, I will discuss how to make choices in some widely used Java Persistence frameworks based on three basic standards: selection, timing, and advantages and disadvantages. In the "select" section, I will introduce how to select a framework. In the "Timing" section, I will discuss some application scenarios where you should consider the application framework and some application scenarios where you should find alternative solutions. Finally, in the "Advantages and Disadvantages" section, I will discuss all the advantages and disadvantages of a framework when you decide to adopt it. The following is the most famous java persistence framework.

  • Entity Enterprise Java Bean
  • Java persistence API
  • Hibernate
  • Toplink

Let's discuss these frameworks in more detail.

Entity Enterprise Java Bean

Enterprise JavaBean (EJB) technology is a managed server-side component architecture for Java Enterprise Edition (Java ee. In this definition, "hosting" and "server side" are key terms. In the EJB architecture, the application server manages the lifecycle of one or more deployed ejbs and provides public runtime services through the EJB container. The services provided by the container include security, concurrency control, transaction and persistence management.

The EJB specification defines three types of enterprise Bean:Session,EntityAndMessage Driven. Each type has some unique features for different purposes. Since this article is about the Java persistence framework, we will briefly discuss the persistence of the EJB architecture, which will involve the persistence requirements for using entity EJB to manage your Enterprise Java applications.

When designing an application using EJB, EJB represents an entity in the business domain model. For example, imagine a commercial bank applicationAccountEntity. TheAccountEntity Bean will be deployed to the J2EE application server, which will provide some runtime services to the Entity Bean through the EJB container. This kind of service is automatically persistent and I will discuss it in more detail.

According to the persistence implementation method, entity EJB can be further divided into two types: bean-managed persistence (BMP) and container-managed persistence (CMP ). When BMP is used, the bean instance is responsible for its State persistence through JDBC code. Using container-managed persistence, the EJB container provides the ability to automatically manage the persistence of entity beans: Save the status to the underlying database or load the status from the underlying database when necessary.

Now let's discuss some application scenarios. In these application scenarios, you may want to apply the EJB framework. In particular, you may use CMP for data persistence.

When to consider using EJB as a persistence framework
  1. First, you need to consider the services provided by the container. If your applications require services provided by containers other than persistent management, such as switch management, security, and concurrency control, it is best to use the EJB framework.

  2. In addition, resource requirements must be considered. The EJB framework can provide perfect scalability for applications. However, this is also costly: intensive resource requirements, especially when using the remote interface mode. You can only apply entity EJB when there is no resource limit to obtain the required performance level.

  3. Another related factor is:De factoFramework. If you are deploying an EE Based on the Java platform, the chance is that the EJB container has become part of your application server. "Why not use an available one ?" It may be the first question you need to ask when making your architecture decisions. I regard this as a "political" advantage over other frameworks because it is available.

When to consider using EJB alternatives as a persistent framework
  1. The functional requirements of the persistence layer may be the first factor to prompt you for an alternative solution. If your application does not require each function provided by the EJB framework, you should seek an alternative solution.

  2. If the resource availability exceeds the performance requirement, it may be another situation where you want to seek other alternatives. Although the EJB framework provides perfect performance and scalability, the consumption of resources by the EJB framework is amazing. The bottom line is: Do we really need this? In many cases, a well-written data access object or Hibernate framework can provide a perfect alternative.

  3. Although few, data sources (rather than relational databases) may prevent you from using the persistence framework of container management.

If you have used or plan to use EJB as your persistence framework, here are some advantages and disadvantages you can expect.

What are the advantages of entity EJB?
  1. This component-based distributed model makes it independent from the network-the EJB component can be deployed on the same JVM that provides services for other applications, or the JVM of the application server located in different geographic locations.

  2. You will get the perfect Scalability: EJB can be well scaled up, Because containers can aggregate instances and activate and deactivate them if necessary.

  3. After a long period of test, EJB has become a mature technology. Moreover, it has been developed to provide more useful services and functions. For example, the timer service is one of my favorite services. With it, you can plan jobs to run at specified intervals (such as every night, every week, or every month ). In EJB 3.0, there is also a useful function "Java Metadata Annotation support", which eliminates all interfaces required for Object Persistence and enhances the query language of EJB.

What are the disadvantages of entity EJB?
  1. Learning and using the EJB architecture is not simple. You should prepare to learn some terms, suchRemote Interface),Home interface (home interface),Activation),Passivation (passivation)And so on. Most of them are only applicable to the EJB world.

  2. The EJB architecture does not provide persistence independence. Because these classes are used in EJB containers in their own way, there is no simple method to use EJB classes in other frameworks.

  3. For entity beans, it is always a challenge to obtain an acceptable performance level, especially in remote interface mode.

Java persistence API

Since the application of EJB technology can be started, there is always doubt about its availability in actual applications. In my opinion, the two most important reasons for this phenomenon are complexity and resource-intensive. As a result, a framework (such as spring and hibernate) that is simpler than EJB and has a smaller resource space, and becomes more popular. To illustrate this, we have noticed that the direction of the EJB 3.0 specification is a major change from the previous one. As part of JSR 220, This SPECIFICATION provides functions such as plain old Java object (pojo) support, dependency injection (dependency injection), and annotation. Now we have introduced a new set of Apis: Java persistence API (JPA) to allow developers to manage the relational data in Java EE (or even SE) applications. In addition, Sun claims that the Java persistence API shows some of the best ideas in the hibernate, toplink (both will be discussed later), JDO, and EJB frameworks.

Currently, the glassfish project provides a reference for implementing JPA. JPA serves as the toplink essential part of the glassfish application server. You can find the JPA reference implementation on the glassfish community page. Do not confuse toplink essenessenand toplink. The former is now a relational ing tool owned by Oracle Corporation. I will discuss the toplink framework later in this article.

Let's discuss some scenarios where you should consider using JPA as a persistence framework.

When to consider using JPA as a persistence framework
  1. You can choose from popular frameworks (such as Hibernate, toplink, and EJB) to select a standard framework with "Easy to use" features.

  2. You need a lightweight persistence framework without the services provided by the EJB container.

  3. You need a persistence framework that can be used in standard or Enterprise Java applications.

When to consider JPA alternatives
  1. The Java version you use determines whether JPA can be applied. JPA is part of the EJB 3.0 specification, which is part of Java ee 5. If you have not updated Java ee 5, you cannot use JPA.

  2. Your application requires services that cannot be provided by JPA, such as those provided by the EJB container, in which cases you are more dependent on EJB.

Before we end the discussion on this framework, let's list some advantages and disadvantages of using JPA as a persistence framework.

What are the advantages of JPA?
  1. JPA is based on standards. More and more providers are looking forward to providing JPA implementation in the near future.

  2. It provides the best functions in Hibernate and toplink.

  3. It can be used together with Java SE and Java EE applications. you need to use an EJB container or not.

What are the disadvantages of JPA?
  1. Because it is very new, the JPA specification may have to go through important developments to become very stable.

  2. JPA is a specification rather than a product. You need the provider to provide an implementation to obtain the advantages of these standard-based APIs.

Hibernate

Hibernate is an object persistence framework that simplifies object relationship ing between Java applications and underlying relational databases. The method is to provide transparent persistence of pojo, as the "intermediary" layer to provide automatic persistence, and load objects to database tables from Java applications. Using hibernate, it is as easy to save the object state to the database and load the object state from the database as calling the method in the Java object. You do not need to manage underlying data operations from your application code. The Hibernate framework completes all the intermediate steps for you.

Let's discuss some application scenarios where you will consider using hibernate as a persistence framework, and those scenarios where you will seek alternative solutions.

When to use hibernate as a persistence framework
  1. You are looking for a simple persistence framework that is easy to learn and use. Before you can actually start to persist your Java object to the target database, you only need to know a few ing configuration files.

  2. You are looking for a very common and flexible persistence framework. The usage of Hibernate is very flexible: it can be used no matter whether there is an Application server or not, whether it is related to a database system or not.

  3. You do not want to pay for access and maintenance. Hibernate is open-source and free of charge.

The Hibernate framework is very useful because it is very simple, flexible, and powerful. However, you may want to apply other frameworks in the following scenarios.

When to consider the alternative framework of Hibernate
  1. You do not want other frameworks. Although simple, The Hibernate framework still has its own learning curve, maintenance/update cycle, and so on.

  2. You need services provided by containers, such as those provided by ejbs. In those cases, your selection is limited to EJB.

If you are using or planning to use hibernate as your persistence framework, here are some of its advantages and disadvantages.

What are the advantages of hibernate?
  1. Hibernate is easy to learn and use. As I mentioned above, before you can use it, you only need to understand a few simple and self-described configuration files.

  2. It is very flexible. You can use hibernate in any application architecture that requires persistence services. You can use Servlet and/or Enterprise Java Bean in standard Java applications and Enterprise Java applications. It can also be well integrated with the Spring framework.

  3. It can be well scaled up because it is designed to work from the underlying layer to the cluster environment. Through lazy Initialization Technology and the optimization of Java reflection through the cglib runtime byte code generation library, the performance of the latest version of hibernate has also been enhanced.

What are the disadvantages of hibernate?
  1. Hibernate is another framework with its own application and maintenance cycle.

  2. Despite active community support, sometimes the lack of providers dedicated to this product makes it unconvincing to publicize and apply this framework.

Toplink

Toplink is another object relationship ing framework of Java. It provides a powerful and flexible framework for storing Java objects to databases and XML documents and loading objects from databases and XML documents. After several mergers and acquisitions (refer to the history on the Wikipedia page of toplink), toplink has become part of Oracle fusion middleware since 2002.

In 2006, Oracle donated source code from toplink products and development resources to the java.net glassfish project. This product is named toplink essenessenand serves as a reference implementation for Java ee ejb 3.0 JPA. It is an extended version of Oracle's toplink product and removes some features, such as cache synchronization between cluster applications, cache verification policies, and query cache. In 2007, Oracle donated the source code from toplink products and development resources to the open-source eclipselink project.

Here are some scenarios where you may want to use toplink as a persistence framework, and some scenarios where you want to find alternative solutions.

When to use toplink as a persistence framework
  1. Although toplink can work with other software systems, it is better if your software system uses Oracle software products because it can build a homogeneous software product suite from the same provider.

When to consider toplink alternatives as a persistence framework
  1. You are a non-Oracle store. If you only have a small number of Oracle software, you can have more options that suit your needs. This may be very typical for EE-based application servers, because websphere, JBoss, and WebLogic are the top three application server providers in terms of market share while writing this article.

Finally, let's evaluate the advantages and disadvantages of using toplink as the persistence framework.

What are the advantages of toplink?
  1. If you have selected a large number of Oracle products in the software queue, toplink is the ideal persistent framework.

  2. It is a very mature framework supported by Oracle and has passed time detection.

  3. Its advanced functions, such as cache synchronization between cluster applications, cache verification policies, and query cache, make it ideal for applications that require high performance and clusters.

What are the disadvantages of toplink?
  1. It is private; its future direction is determined by Oracle.

  2. Like using any new framework, it also has its own learning curve.

Persistence framework selection list

Before continuing, let me summarize the framework discussed above in the following table. In this table, you will see the General Environment (timing), the Framework (selection) You should consider, and your advantages and disadvantages (advantages and disadvantages ). You should regard these as the starting point for choosing the persistence framework process. Your final choice should be based on these and other application-specific requirements (if any ).

Select?
(Considering applications)
Time?
(Is your application required)
What are the advantages?
(You will obtain these advantages)
What are the disadvantages?
(You will obtain these shortcomings)
Java persistence API Simple persistence framework for standard or Enterprise Java applications
  • It is based on standards.
  • It combines the "Easy to get" features of many other frameworks.
  • It is a specification that requires the implementation of specific suppliers.
  • It cannot be used in versions earlier than Java 5.0.
Entity EJB of Container Management Services provided by containers, such as security and switch management, and persistent management.
  • Distributed Components
  • Good scalability
  • Resource intensive
  • Complex learning and use
  • Poor flexibility
Hibernate You want a simple and flexible framework
  • No maintenance fee required
  • Good integration with other frameworks
  • Easy to learn and use
  • Flexible: available with or without ejbs, available in standard or Enterprise Java
  • Open Source
Toplink Your software system has used a large number of Oracle Products
  • Mature technology
  • Vendor-specific
Other Persistence frameworks

Before the end, I listed some other Persistence frameworks. Before applying a framework, these frameworks are worth exploring. This article will not discuss these frameworks in detail.

  • CASTOR: a free open-source data binding framework.
  • Kodo: Object link persistence engine of BEA Systems.
  • Torque: a Java-based object-link er developed as an Apache dB project.
  • Ibatis: a data ing framework that can be used together with Java and. NET applications.
Conclusion

Obviously, which persistence framework you will apply for the next Java application will be affected by many factors, for example, required functions, acquisition and maintenance costs, and non-functional requirements (maintainability, performance, and so on ). Some private and open-source frameworks are now available, each of which has its own advantages (benefits) and disadvantages (disadvantages ).

In this article, I have explained some popular Java Persistence frameworks based on the three standards: Which one is the application (definition Framework), when to apply (when should we consider the application and when should we look for alternatives) And what (advantages and disadvantages) are there ). The application scenarios described in this article are used to help you make correct decisions based on more information before you actually select an application framework. Therefore, you should apply your next Java persistence framework based on the standards discussed in this article and other current and future requirements specific to this application.

References
  • A Comparative Study of persistence mechanisms for the Java platform (PDF)
  • Jsr220
  • EJB product page
  • Hibernate.org
  • Java persistence API
  • Toplink product page
  • BZ research on Java EE market share data

Sharad Acharya has more than eight years of experience in software engineering and involves a wide range of businesses, including supply chain, insurance, banking and mortgage.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.