Dashboard > Struts University > Home > JPA Training Course > JPA Agenda > Subscribe-JPA-Notes
Subscribe-JPA-Notes
Added by Ted Husted, last edited by Ted Husted on Dec 07, 2007  (view change)
Labels: 
(None)


Now that we have it working, let's make it pretty!

Presentation - Annotating Persistence Applications

In this session, we learn how to use annotations to describe object relationships and associations that the JPA cannot deduce on its own.

  • What are annotations?
  • How do we declare properties?
  • How do we declare relationships?
  • How do we declare inheritance?

Some object relationships are subtle, and some relationships are hidden by legacy design conventions.

Workshop - Subscribe

In the lab, we extend the User object to include other properties, including a list of subscriptions.

Story: Subscribers can maintain a set of email subscriptions for an account.

  • Create Subscription entity object
  • Declare a one to many relationship between User and Subscription

Prerequisites

  • User entity object
  • EntityService and EnityManagerHelper

Exercises

  • Review the Subscribe use case
  • Create a Subscription entity and SubscriptionTestCase (or vice versa!)
  • Place Subscription and any helper classes in a entity.subscription package
  • Define a relationship between the User and Subscription entites that says a User can maintain zero or more Subscriptions. This should be a bi-directional relationship, where each entity is aware of the other.

Hint Code

@ManyToOne(targetEntity = UserImpl.class)
    @JoinColumn(nullable = false)
    private User user;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, targetEntity = SubscriptionImpl.class)
    private List<Subscription> subscriptions;

Accomplishments

  • Created Subscription entity object
  • Declared a one to many relationship between User and Subscription

Extra Credit

  • Provide User methods to addSubscription, addSubscriptions, and removeSubscription. Since Subscription owns the relationship, these methods do not need to be persistence-aware. But, the methods should set the Subscription's User to this User instance.
  • Add a Protocol object entity to store whether an account uses POP3 or SMTP. Setup a one to many relationship between Protocol and Subscription. (One Protocol can be used by many different Subscriptions.) This can be a unidirectional relationship, meaning Protocol will not need a link to its Subscriptions.
  • Refactor the entity.subscription objects to use an interface and implementation class. Use the original class name for the interface name, and add an "Impl" suffix to the default class name (e.g. Subscription, SubscriptionImpl, SubscriptionService, SubscriptionServiceImpl).

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators