At least one user library must is selected is actually creating a directory, importing three packages to
①, Eclipselink.jar
②, Javax.persistence.source_2.1.0.v201304241213.jar
③, Javax.persistence_2.1.0.v201304241213.jar
(Note: This series of notes is written when studying the JPA course in Silicon Valley, combining course content and self-understanding to facilitate review later)
First, look at what JPA is before you create a JPA
1. Java Persistence API: API for object persistence
2, the Java EE 5.0 Platform Standard ORM specification, enables the application to access the persistence layer in a unified way
3, combined with the following two pictures to understand:
JPA feature schematic:
Figure 1.1
JDBC Feature schematic:
Figure 1.2
4. JPA and Hibernate relationships
JPA is an abstraction of hibernate (like JDBC and JDBC-driven relationships):
--JPA is a specification: JPA is inherently an ORM specification, not an ORM framework-because JPA does not provide an ORM implementation, it only has some specifications that provide some programmatic API interfaces, but the implementation is provided by the ORM Vendor
--hibernate is implemented: Hibernate is also a JPA implementation in addition to being an ORM framework
Functionally, JPA is a subset of Hibernate functionality
Two, before creating the JPA project need to solve a problem: at least one user library must be selected
Problem Description: Right-click in Workspace, New→jpa Project, select Jap version as 2.0. At this point, the finish in the wizard is grayed out and cannot be clicked. No way, have to click Next to see, all the way next to see the following figure 2.1 shows such a problem: at least one user library must is selected.
Figure 2.1
There is no way to create a JAP project, had to search the web, found a crooked nut to write a blog post, a good solution to my problem:
http://www.javabeat.net/eclipselink-jpa-installation-configuration/
Here is a summary of the main points of the post as follows:
This article will show you how to install and use ECLIPSELINK-JPA:
1. Install the Eclipselink JAR file from https://www.eclipse.org/eclipselink/downloads/download Eclipselink 2.5.2 Installer Zip (+ MB) resource (You can also choose a different version). The implementation of all persisted services is provided in Eclipselink. The persistence service will use the following three Jar packages: ()
①, Eclipselink.jar
②, Javax.persistence.source_2.1.0.v201304241213.jar
③, Javax.persistence_2.1.0.v201304241213.jar extract the downloaded Eclipselink 2.5.2 compression package to any folder you want to use to complete the installation. I unzipped the file into the ". \workspace\libraries\eclipselink 2.5.2" folder.
2. Setting JPA facets in the Eclipse IDE
First create a Dynamic Web project in Eclipse to prepare us for subsequent additions to the JPA facet. The steps are as follows:
①, open Eclipse IDE
②, creating a Dynamic Web project
③, right-click on the project you just created, select Properties
④, click "Project Facets" in the left column
⑤, then select the JPA option on the right, and select JPA version 2.0. Figure 2.2 shows the following:
Figure 2.2
Here is a note, because there are a lot of subsequent settings to complete, so after choosing JPA and version can not directly click OK, but to click on the following section of the image labeled "Further configuration ..." link to make subsequent configuration. This will open a new dialog box to configure the JPA Facet.
3. Adding JPA Facets
The new pop-up dialog box is shown in Figure 2.3 below. The user can add the necessary libraries to this dialog box to implement JPA.
Figure 2.3
①, platform The top of the dialog box is "Generic 2.0" by default, we can modify it according to the eclipselink version we download. I downloaded version 2.5.2, so select platform as figure 2.3 for Eclipselink 2.4.x/2.5.x version
②, JPA implementation menus do not require any modifications. "User Library" type will allow users to add JPA libraries
③, click "Manager Libraries" on the right. In addition, another way to achieve this is to click on the "Download library" to download the library, but it is too slow to download, so do not use it.
④, click "Manager Libraries" will pop up a dialog box, in the New dialog box, click on "".
⑤, "New" will pop up a dialog box, want us to fill in the name of the library, I filled out as "Eclipselink 2.5.2". Click OK.
So far, we are ready to add the jar package. The next step is to add the three jar packages mentioned in "1, install Eclipselink jar file" above. The current renderings are as follows in Figure 2.4:
Figure 2.4
4. Configure the Library
Configuring the library requires us to add the jar packages necessary for the JPA implementation. Follow the steps below to complete the configuration:
①, click "Add External JARs ..." (i.e. the button in the red box in Figure 2.4)
②, add Eclipselink.jar, which is located under the ". \workspace\libraries\eclipselink 2.5.2\jlib" folder
③, add the jar package that starts with javax.persistence.* under the ". \workspace\libraries\eclipselink 2.5.2\JLIB\JPA" folder (that is, the latter two jar packages). As shown in Figure 2.5
Figure 2.5
④, clicking the OK button on the lower end will close the Add Library dialog box.
⑤, however, in order to avoid the JPA facet report "At least one user library must is selected" this error. We chose the library we just created.
⑥, click OK.
Until now, the least one user library must be selected error that was mentioned earlier has been resolved. Here we go back to creating a JPA project with no problem. However, the purpose of this article of crooked nuts is to create a JPA environment in a Dynamic Web project.
This is necessary, think about it, and now just to learn to build a JPA project, in the future when doing web engineering will need to create a dynamic Web project.
And, the following two steps is very interesting, so, here is socialize, continue to translate the article of the crooked nuts.
The Dynamic Web project we created so far has been able to use JPA. However, JPA is a persistent service that is associated with a relational database. So, next we need to create a database connection
5. Create a database link (the new idea is that you can complete the linked test work while configuring)
Once you have created a user library, the next task is to create a database connection. The last dialog box should look like Figure 2.6:
Figure 2.6
To establish a link to a database, follow these steps:
①, click on the "Add Connection ..." hyperlink (with the callout in Figure 2.6)
The ②, Connection Profile dialog box will pop up
③, select the project in the dialog box to select the type of database used by the project. This tutorial assumes that you are using a MySQL database.
④, change the name of the link, click Next
⑤, complete the Specify a Driver and Connection Details form. There is no driver in the dialog that might pop up in the first place, and this time we need to manually add the relevant drivers, as shown in Figure 2.7 and Figure2.8:
Figure 2.7
Figure2.8
After clicking OK in Figure 2.8 above, the new driver is added. Then you modify the database name in Figure 2.9, fill in the user name and password, and then click "Test Connection" to test the success of the database connection:
Figure 2.9
Interestingly, the test will pop up with a ping successed! The prompt box, as shown in Figure 2.10:
Figure 2.10
⑥, at the end of step ⑤, we have a database connection ready for the JPA project
⑦, click the Finish button in Figure 2.9
⑧, clicking Finish will close the dialog box, so make sure the connection is selected
⑨, click, OK to close further configuration dialog. Continue to click OK to close project facets
Now that you've created a new Dynamic Web project, you've configured the JPA environment for it. In the navigation bar on the left side of eclipse you can see a meta-inf folder under SRC, and there is a persistence.xml file underneath the folder.
6. Configuring the Persistence.xml File
If you follow the steps to get here, and all the steps have been successfully performed. Now is the time to "edit" the Persistence.xml file, which will help us connect to our database.
In order to configure the Persistence.xml file, you need to double-click to open it. It will open in a separate XML file editor, and there will be a lot of tags at the bottom of the editor. Now we follow the steps below to complete the configuration of the Persistence.xml file:
①, navigating to connection tags