Original http://www.oracle.com/technetwork/articles/java/springtojavaee-522240.html
By David heffelfinger
CTO and ardent Java EE fan David heffelfinger demonstrates how easy it is to develop the data layer of an application using Java EE, JPA, And the netbeans ide instead of the Spring framework.
Published October 2011
Introduction
Followers of the Spring framework claim that they have chosen a much better framework than Java ee. I am undoubtedly a fan of Java EE and have written several books to detail this technology. However, like most developers, I do not always directly choose Java, and sometimes choose Spring Based on the situation.
Every time I work on a spring Project, I get nervous because I know that I will use lengthy and confusing XML files to determine the behavior of the project. I also know that this project has almost 10000 dependencies and the war generated is even more a giant.
When using Java EE, most of the services I need are provided by the application server. Therefore, few dependent projects are required. In most cases, Java EE provides configuration (with a few exceptions), that is, I need to do very little configuration, because the default configuration can meet the needs of the vast majority of situations. Annotations are usually used for configuration. Annotations allow me to view the project panorama only by looking at the code without going back and forth to view the XML and source files.
In addition to the benefits mentioned above, using Java EE to build a project also benefits from the advanced tool netbeans. And if I am lucky enough, use the open-source glassfish server or Oracle glassfish server as my server, you can use the "Save deployment" feature (automatically deployed to the glassfish server every time you save the project file ).
This series will use Java EE to override the pet clinic application provided by spring. This is the first article. I will illustrate how to use the awesome netbeans to quickly develop and use applications with the same features of the spring version. This Java EE version uses the assumerver faces (JSF) as the user interface, and the Data Access Object (DAO) is implemented using the ejb3.1 Session Bean. Data access is provided by Java persistence API (JPA) 2.0.
In this article, we will use an existing database to generate a persistent layer to start developing our Java EE version. In the second part, we will see how netbeans helps us generate the ejb3.1 Session Bean as Dao, and jsf2.0 as the user interface.
Here we assume that MySQL is installed on the local workstation, and the petclinic database already exists. (Easy to create, just run the setupdb ant target in Pet Clinic .)
First create a web project, as shown in figure 1.
Figure 1. Creating a new project
Specify the name and location, as shown in Figure 2. Generally, you can use the default value.
Figure 2. specifying a name and location for
New project
Now we select the framework. We use standard Java EE, So we choose JavaServer faces,
As shown in figure 3.
Figure 3. Selecting assumerver faces as the framework
Now select the server and Java EE version, as shown in Figure 4. The default value is enough.
Figure 4. Selecting the server and Java EE version
Click Finish to complete the creation, as shown in Figure 5.
Figure 5. The newly created Project
Start development.
Developing the application
Netbeans generates most of the Code required to develop our Java EE application. It can help us generate JPA entities, Dao, JSF pages, and JSF managed beans.
First, we need to develop our JPA entities. Most JPA implementations include the ability to generate database tables from JPA entities; however, in turn, this is not acceptable. JPA does not provide the ability to generate JPA entities from database tables.
In most cases, you need to manually write JPA entities and add annotations, attributes, Getter, and setter. However, you don't need to use netbeans. It can automatically generate JPA entities from the data perspective. Select File
| New, select entity under the persistence directory
Classes from database, as shown in figure 6.
Figure 6. Selecting entity classes from database
Now you need a data source. If not, you can create it on the spot. As shown in figure 7.
Figure 7. Creating a data source
Just enter Java Naming and Directory Interface (JNDI)
Name: as shown in figure 8.
Figure 8. Selecting a database connection
Once again, if no database connection is established to the target database, it can be created at any time through the wizard. The first thing after the creation is to specify the JDBC driver, as shown in figure 9.
Figure 9. Selecting a driver
Specify the host, port, database, and password, as shown in Figure 10. Remember to click test.
Check whether the connection configuration is correct. If yes, you will see "connection succeeded ".
Figure 10. specifying additional details and Testing
The Connection
If MySQL is used, the schema is the database. Therefore, the Select schema is gray,
As shown in Figure 11.
Figure 11. Selecting a schema
Click Finish to create a database connection. Click OK until the new entity classes from database screen is returned. Figure 12.
NB tries to guess the object class name through the database table name. The petclinic database uses the plural table names (such as owners, pets, specialties), but we want to use the corresponding singular nouns (such as owner, pet, specialty ). Haha, Nb has this function: it allows you to modify the default JPA object class name. Double-click the name here.
Now you can generate name queries, jaxb annotations, and persistence units for each field of the JPA object. In most cases, it is a good idea to select one or three items. We may not use the jaxb annotation, but it will be okay if we select it.
Figure 12. Specifying entity classes
Click Next to specify mapping options, as shown in figure
13.
InAssociation fetchColumn, you can select how to associate and load objects. By default, one-to-one, multiple-to-one, one-to-multiple, and multiple-to-many lazy loads are supported. You can select either of them, which is the best by default in most cases.
Figure 13. specifying how associated entities are
Loaded