当前页面: 开发资料首页 → Netbeans 专题 → Using Java Persistence in a J2EE 1.4 Web Application
摘要: This document takes you through the basics of how to add Java? Persistence functionality to a J2EE 1.4 web application. Though the Java Persistence API was introduced as part of the Java EE 5 platform, it is possible to use Java Persistence in a J2EE 1.4 application. The target container needs to be running on JDK 1.5, but it does not need to be a Java EE 5 container
This document assumes you have some basic knowledge of, or programming experience with, the following technologies:
For this tutorial you need to have the following software installed on your computer:
This tutorial shows you how to create the ZooApp application in an environment where there is no local installation of the Sun Java System Application Server or Java DB database server. Instead, we will use the bundled Tomcat web server as our container, and use a locally installed database server, in this case a MySQL database. The steps for setting up a database connection are also applicable to setting up a JDBC connection to other JDBC-compatible database servers, such as an Oracle database.
Before we can create our application, we need to confirm that the IDE and software are configured correctly and make sure that the database connection to our database is working correctly. We need to do the following:
We have to make sure that the Java platform for the IDE and the Tomcat web server are set to JDK 1.5.
In this example we use a locally installed MySQL database, but you can use any database that uses a JDBC driver. The IDE does not come with a database connection to the MySQL database, so we need to create a database connection in the Runtime window.
We need a MySQL JDBC driver to create a database connection to the MySQL database. If there is no MySQL JDBC driver, we need to add one.
Note: When you add a JDBC driver in the Add JDBC Driver dialog, you are providing the information necessary to allow the IDE to comunicate with the database. For example, this allows you to view the contents of a database in the Runtime window of the IDE. You may still need to add the appropriate driver to the web server library to allow the server to communicate with the database.
When you click OK, a new database connection node appears under the Databases node. If there is no open connection to the database, right-click the database connection node, choose Connect and supply the username and password, if necessary. We can test our connection by expanding the connection node to see the tables in the database, if any. Now that we have established a connection to our database, we can start creating our web application.
In this exercise we made sure our IDE and web server were running on the correct Java platform and that we had a working database connection to our database.
In this exercise we create the ZooApp web application project. When creating the project, we first specify the project properties such as the J2EE version, the Java source level and the target server. We then need to configure the classpath of our project so that the appropriate JAR archives are available when we build and deploy the application.
Because we are deploying to Tomcat instead of a Java EE container, we need to set the J2EE version to J2EE 1.4 when we create our project. But we need to set the source level of our project to 1.5 so that can use Java EE 5 functionality in our source code.
When you click Finish, the ZooApp web application appears in the Projects window of the IDE.
In this tutorial we want to use Java Persistence in the Tomcat web container. Because the Tomcat web container is not a Java EE 5 container, we need to add support for Java Persistence by making sure the appropriate libraries are on the classpath. You add support for Java Persistence by adding the TopLink Essentials library to the classpath of your project. The TopLink Essentials library is bundled with the IDE and is located in <NETBEANS_HOME>\enterprise3\modules\ext\toplink, where <NETBEANS_HOME> is the installation directory of the IDE.
We now need to add the JAR archive for our database driver to the classpath.
In this exercise we created a J2EE 1.4 web application. We then added the TopLink Essentials library and the database driver to the classpath of our web application. We can now start creating the entity classes and the web interface. The following steps are the same as in the Java Persistence in the Java EE 5 Platform tutorial.
In this exercise we will create two entity classes, Animal.java and Pavilion.java, that represent the tables in the relational database we want to create. We will also use annotations to define some fields in the classes to represent the data.
We need a persistence unit in order to persist entity classes. The persistence unit specifies the datasource, the entity classes to be persisted and the entity manager for managing the life-cycle of the entities. The Tomcat server does not support container-managed persistence so we will use an application-managed entity manager. The entity manager must be specified as resource-local because Tomcat does not support JTA.
When we use the New Entity Class wizard, we are prompted to create a persistence unit if our application does not have one.
First we will create the entity class Animal. This class represents the ANIMAL table in our database. When you create the entity class, the IDE adds the @Entity annotation to define the class as an entity class. After we create the class, we will create fields in the class to represent the data that we want in our table, and use annotations to provide additional information about some of the fields.
Each entity class must have a primary key. When you create the entity class, the IDE adds the @Id annotation to declare which field to use as the primary key. The IDE also adds the @Generated annotation to specify the key generation strategy for the primary Id.
To create the Animal class, do the following:
When you click Finish, the new entity class Animal.java opens in the Source Editor. In the Source Editor, do the following:
String name; String kind; String weight; Pavilion pavilion;
@Column(name="animalName") private String name;
@ManyToOne private Pavilion pavilion;
The error highlighting in the IDE indicates that our class still has some errors, but this will be resolved after we create the Pavilion entity class in the next step.
We now will create the entity class Pavilion representing the PAVILION table in our database. We will again use annotations in our class to specify the object-relational mapping of some of our fields. To create the Pavilion class, do the following:
When you click Finish, the new entity class Pavilion.java opens in the Source Editor. In the Source Editor, do the following:
String name; String address; Collection <Animal> animals;
@Column(name="pavilionName") private String name;
@OneToMany(mappedBy="pavilion") private Collection <Animal> animals;
In this exercise, we created two entity classes and defined some of the fields in the classes. We also used annotations to define the properties of some of the columns in the tables that will be generated when the application is deployed. We also created a persistence unit with details required for managing the entities in our application.
We now want to create some simple web pages to see if our database tables were created and if we can add data. We will add Java Server Faces (JSF) pages to the application and use the JSF Pages from Entity Class wizard to quickly create a simple web interface.
When you click Finish, the IDE generates the required JavaServer Faces files so that we can run and test our ZooApp.
In this exercise we will deploy our ZooApp web application project and test our application.
When you click Run, a page opens in your browser with a menu enabling you to see a list of the pavilions and animals.
You can also add, edit or delete the data for animals and pavilions.
In this exercise, you built the ZooApp web application. You then deployed and tested the ZooApp web application.
The following are some of the problems you may encounter when creating your project.
When using the wizard to create JSF pages from an entity class, you may see the following error message in the wizard:
This wizard can only be used in a web project with JSF support.
If you see this message you need to check that the Java Server Faces framework has been added to the project properties. You can add Java Server Faces support to your web project by doing the following:
After adding the JSF Framework to the project properties, you should be able to create the JSF pages using the wizard.
If you can connect to the database from the IDE but you get a message that the server is unable to connect to the database, you may need to add the JDBC driver to the web server library. You should consult the documentation for your server for the correct location.
For example, to add the MySQL driver to the Sun Java System Application Server, you should add the mysql-connector-java-5.0.4-bin.jar to the APPSERVER_HOME/domains/domain1/lib/ext/ directory, where APPSERVER_HOME is the installation directory of the server.
For more information about using NetBeans IDE 5.5 to develop Java EE applications, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.