Converting a project into a JPA project

Many JPA tools become active in Eclipse JEE only if the project is a JPA project. Although we have created a Maven project, it is easy to add an Eclipse JPA facet to it:

  1. Right-click on the project and select Configure | Convert to JPA Project:
Figure 4.20: Addding a JPA facet to a project
  1. Make sure JPA is selected.
  2. On the next page, select EclipseLink 2.5.x as the platform.
  3. For the JPA implementation type, select Disable Library Configuration.
  1. The drop-down list for Connection lists any connections you might have configured in Data Source Explorer. For now, do not select any connection. At the bottom of the page, select the Discover annotated classes automatically option:
Figure 4.21: Configuring a JPA facet 
  1. Click Finish.
  2. Notice that the JPA Content group is created under the project and persistence.xml is created in it. Open persistence.xml in the editor.
  3. Click on the Connection tab and change Transaction type to Resource Local. We have selected Resource Local because, in this chapter, we are going to manage EntityManager. If you want the JEE container to manage EntityManager, then you should set Transaction type to JTA. We will see an example of the JTA transaction type in Chapter 7, Creating JEE Application with EJB.
  4. Enter EclipseLink connection pool attributes as shown in the following screenshot and save the file:
Figure 4.22: Setting up Persistence Unit Connection
  1. Next, click on the Schema Generation tab. Here, we will set the options to generate database tables and relationships from entities. Select the options as shown in the following screenshot:
Figure 4.23: Setting up Schema Generation options of Persistence Unit

Here is the content of the persistence.xml file after setting the preceding options:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 
  <persistence-unit name="CourseManagementJPA" transaction-type="RESOURCE_LOCAL"> 
    <properties> 
      <property name="javax.persistence.jdbc.driver" 
value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost/course_management_jpa"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.schema-
generation.database.action" value="create"/> <property name="javax.persistence.schema-
generation.scripts.action" value="create"/> <property name="eclipselink.ddl-generation" value="create- tables"/> <property name="eclipselink.ddl-generation.output-mode" value="both"/> </properties> </persistence-unit> </persistence>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.10