Jalapeno

Recent site activity

Exercise 2: Add Persistent Metadata (JPA Version)


Previous Table Of Contents
 Next

Exercise 2: Add Persistent Metadata

Step 0: Enable JPA Mode


  • If you have not yet enabled JPA Mode you need to follow a few simple steps now
  • First, enable JPA Mode for Plugin. See here how to do it.
  • In Jalapeño Tools Menu, select Manage Database Schema -> Make All Persistent Classes JPA Entities (If you did Exercise 1 in JPA mode they are already annotated).
    • You may see a warning that J2EE library is not a dependency for the module. In that case click on "Add Dependency" button: it will add a dependency to module definition, it will not download or physically copy any jars or other files.

Step 1: Create a Primary Key


  • Switch to window for class Hospital and look at IDE warnings
    • Sometimes IDEA does not recognize that code inspections has to be rerun. In this case to see the warnings you will need to edit a file and then undo your edit. Hitting Enter with undo usually helps while in IDEA 7.x adding a whitespace does not - IDEA outsmarts itself.
  • Choose a QuickFix to annotate getName as ID
  • A new annotation is created

Step 2: Establish Relationship

  • Turn to a yellow warning block around getDoctors() method
  • Select QuickFix to Establish Relationship
    • If you do not see a QuickFix to Establish Relationship (because it is hidden by JPA validation error) go to Jalapeño Tools Menu, select Annotate -> Establish Relationship
  • The only option for inverse property is getHospital() method in class Doctor

Step 3: Analyze Relationship

  • Look at the new annotation added
  • Switch to class Doctor and look at getHospitals() method: it is also annotated now

Step 4: Create Composite Unique Constrain


  • None of Doctor's properties can serve as primary key thus we will use Database Id as Id and add a unique constrain based on name and address
  • Select "Create New ID property" for QuickFix.
  • A new field is added to class Doctor and annotated as Database Id.
  • Put cursor on method getName() and  go to Jalapeño Tools Menu, select Annotate -> Property As Unique
  • An annotation for a unique index is created
  • Edit propertyNames element of index to type in additional properties, e.g. address.street, address.postalCode. Note that text turns red when property name is incomplete.
  • You can use autocompletion by hitting Ctrl-Space
    • Instead of using Jalapeño @Index annotation you can use JPA  @UniqueConstraint one. However IDEA Jalapeño plugin does not provide any specific help for creating it. Since it is standard JPA annotation one can argue that it is not responsibility of the plugin but rather of the IDE itself.
  • Optionally, you can add a bitmap index to speed up search for a doctor with given speciality in a given postalCode

Step 5: Additional features

  • Switch to Address class, go to Jalapeño Tools Menu, select Annotate -> Class as Embeddable
  • Address should be now green, i.e. display no warnings
  • Selecting package data go to Jalapeño Tools Menu, select Annotate -> Class as Populatable to enable automatic population with test data

Step 6: Advanced population – define Population Spec

  • Jalapeño is smart enough to infer how to populate test data from your property names. But if needed you can override this behaviour
  • In class Hospital put cursor on getName() method, Open Jalapeño Tools Menu, select Annotate -> Property with POPSPEC
  • Select Company() spec
  • In class Doctor annotate getSpeciality with POPSPEC ValueList
  • Replace Value with something like  "ValueList(\",PCP,Mental Health,Surgeon,Cardiology, Internal Medicine\")"

Step 7: Build It!

  • First we need to delete the data we have inserted in the database by running test program in Exercise 1
  • Go to Jalapeño Tools Menu, Data Management -> Clean All Data from the Database and wait for the confirmation
  • Then rebuild Database Schema.
    •  When building schema at this stage you must either ensure that "Delete Old Schema Before Build?" checkbox is checked in Jalapeño Settings Panel or delete old schema using Schema Management submenu of Jalapeño Tools menu.

Step 8: Populate with Test Data

  • Go to Jalapeño Tools Menu, Data Management -> Populate All Classes…
  • Wait for the confirmation
  • Open Database Schema either in a browser as you did in Exercise 1 or in your favourite database viewer (Squirrel, WinSQL, JaySQL, DBVisualizer, etc) and see data there
  • Note that there are only two tables now (Doctor and Hospital) because Address is now embedded

Step 9: Run test program

  • Before running test program you might need to create persistence.xml descriptor if you have not yet done so. See here how to do it. However if you did Exercise 1 in JPA mode than descriptor is already there.
  • Run the same test program you ran at the end of exercise 1. You should see output like on the picture