Port of schuchert.wikispaces.com


JPA_Tutorial_3_Project_Setup

JPA_Tutorial_3_Project_Setup

For the rest of this section, when you see ****, replace it with **JpaTutorial3**


title: JPA_Tutorial_Project_Setup —

Create Java Project

Next we need to create a Java project. We’ll keep the source separate from the bin directory:

  • Pull down the File menu and select New:Project
  • Select Java Project and click on Next
  • Enter a project name: ****, again read [this](JPA_Tutorial_1_Getting_Started#SideBarJpaClassPath) to know why I did not use a space in the project name.
  • Make sure “Create new project in workspace” is selected.
  • Make sure the JRE selected is 1.5.x or higher. If such a JRE does not show in the list, you can add it through Window->Preferences->JAVA->Installed JRE’s.
  • Select Create separate source and output folders
  • Click Finish

Create folders and packages

  • Expand your **** folder
  • Select the src directory
  • Right-click, select new:Folder
  • Use the name META-INF
  • Make sure **** is still selected, right-click and select **New:Source Folder**
  • Enter test and click OK

Add Required Libraries

We now need to add two libraries. Note that these steps assume you’ve already worked through the first tutorial and are working in the same workspace. If you, you’ll need to create user libraries. Review Creating User Libraries.

  • Edit the project properties. Select your **** and either press **alt-enter** or right-click and select properties.
  • Select Java Build Path
  • Click on the Libraries tab
  • Click on Add Library
  • Select User Libraries and click Next
  • Select JPA_JSE by click on the check box
  • Click OK
  • Click on Add Library again
  • Click on JUnit
  • Click Next
  • In the pull-down list, select JUnit 4
  • Click Finish
  • Click OK

If you’d like some background information on JUnit, please go here.

Configure Persistence Unit


title: JpaPersistenceUnit —

We now need to create the Persistent Unit definition. We are going to create a file called persistence.xml in the src/META-INF directory with the following contents:

persistence.xml

<pre><code class="language-xml" data-lang="xml"><span class="nt">&lt;persistence&gt;</span>
<span class="nt">&lt;persistence-unit</span> <span class="na">name=</span><span class="s">"examplePersistenceUnit"</span> 
                  <span class="na">transaction-type=</span><span class="s">"RESOURCE_LOCAL"</span><span class="nt">&gt;</span>
    <span class="nt">&lt;properties&gt;</span>
        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.show_sql"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.format_sql"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/&gt;</span>

        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.connection.driver_class"</span> 
                  <span class="na">value=</span><span class="s">"org.hsqldb.jdbcDriver"</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.connection.url"</span> 
                  <span class="na">value=</span><span class="s">"jdbc:hsqldb:mem:mem:aname"</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.connection.username"</span> <span class="na">value=</span><span class="s">"sa"</span> <span class="nt">/&gt;</span>

        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.dialect"</span> 
                  <span class="na">value=</span><span class="s">"org.hibernate.dialect.HSQLDialect"</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"hibernate.hbm2ddl.auto"</span> <span class="na">value=</span><span class="s">"create"</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/persistence-unit&gt;</span> <span class="nt">&lt;/persistence&gt;</span></code></pre>

The Steps

  • Expand your ****
  • Select the src directory
  • Find the src/META-INF directory (if one does not exist, right-click, select New:Folder, enter META-INF and press enter)
  • Right click the src/META-INF, select new:File.
  • Enter persistence.xml for the name and press “OK” (Note: all lowercase. It won’t make a difference on Windows XP but it will on Unix.)
  • Copy the contents (above) into the file and save it

Update persistence.xml

Update Persistence Unit Name

The name of the persistence unit in your just-copied persistence.xml is examplePersistenceUnit in this example we use lis for Library Information System. Make the following change:

    <persistence-unit name="examplePersistenceUnit" 
                      transaction-type="RESOURCE_LOCAL">
    <persistence-unit name="lis" 
                      transaction-type="RESOURCE_LOCAL">

Your project is now set up.


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.