Enterprise web programming

Modules ENTWA (Level 6) and APSW (Level 7)

Step by step JPA/JSF webapp 2

Create more entity classes

  1. In your entity package, create a second business object class. As before, use the New File | Persistence | Entity Class wizard in NetBeans. Normally use a surrogate key of type Long as the primary key.
  2. Note @Entity, @Id and @GeneratedValue annotations.
  3. Add properties to the entity and create setters/getters for each.

Create relationship between classes

  1. We will assume that we want to create a relationship with cardinality "many-to-one" between objects of class A and class B.
  2. In class A, declare a property of type B with a suitable name.
  3. In NetBeans, this should cause a lightbulb to come up in the left margin. Clicking the lightbulb will get a list of options. Choose the appropriate cardinality. Almost always you will want to establish a bidirectional relationship.
  4. The wizard that comes up will ask you to associate this relationship with a (possibly new) property in class B. If it's new, give it an appropriately meaningful name (in this case, the default is not often a good choice because it is simply based on class A's name).
  5. When you click on OK, the wizard will create the property in class B and put the appropriate annotations on the declarations in both class A and class B. For cardinality many-to-one, the property in class B will be of type List<A> indicating that each B object can be related to many A objects.
  6. If they don't already exist, you will need to create getters and setters for the properties in both A and B.

Add functionality to business classes and controllers

  1. Amend all appropriate business functions to ensure that they:
    1. use the persistence layer to merge (edit) any existing objects that are used from the presentation layer
    2. set up both parts of any bidirectional relationships (and remove any unused relationship)
  2. Ensure that appropriate additional properties are created in all appropriate controllers.
  3. Amend view accordingly.
 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The enterprise web programming modules include some material that was formerly part of the WEB1P and WEB2P units.