This is an introduction to JPA


Slide Deck

Section 1: Intro to JPA


Lab 1

  • Download the JPA Training project template
  • Unzip the artifact where you typically keep training or other projects
  • Open the jpa-training folder as a workspace in your LDS Tech IDE
    • if you end up with double folders... Jpa-training/jpa-training, open the deepest one as your workspace
  • Import the jpa project (and sub-projects)
    • File -> Import
    • Expand Maven
    • Choose "Existing Maven Projects"
    • point it to ...jpa-training/jpa as the root directory (allow it to import the sub projects)
    • click finish and wait for it to complete (activity is shown in the lower right corner)
  • Open applicationContext.xml found in jpa-web/src/main/resources/META-INF/spring
  • Un-comment the stack-db:hibernate entry - make sure the persistence unit name matches the one in jpa-web/src/main/resources/META-INF/persistence.xml
    • you may want to switch to the "souce" tab at the bottom of the editor window
  • Open web.xml found in jpa-web/src/main/webapp/WEB-INF/
  • Un-comment the OpenEntityManagerInViewFilter
  • Finish implementing ExampleServiceImpl.createExample(..) using a call to entityManager.persist(..)
    • Open (CTRL-SHIFT-T ExampleServiceImpl)
    • add code to persist the "example" object that is passed in
  • Test your solution by running the application and creating a new example object
    • start your jpa-1.0 (Tomcat...) server and navigate to http://localhost/jpa/example
    • add some examples using the form
    • verify your additions show up in the list on the right
    • stop your server

Lab 1 Solution

Section 2: Querying


Lab 2

  • Finish implementing ExampleServiceImpl.findExampleByName(String name) using a JPQL query
    • add code to query and return the example object by name (the one passed in) using JPQL
    • for this lab you can assume the name will be unique (although not a great practice in the real world)
	return entityManager.createQuery("from Example e where = ?", Example.class)
		.setParameter(1, name)
  • Test your solution by running the ExampleIT integration test
    • Navigate to found in /src/test/java/org/lds/stack/training
    • Right-click on the file and choose "run as" -> testNG test
    • verify that one test ran with zero failures

Lab 2 Solution

Section 3


This page was last modified on 21 June 2013, at 11:13.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.