concept Order in category spring

appears as: Order
Spring in Action, Fifth Edition

This is an excerpt from Manning's book Spring in Action, Fifth Edition.

Now that you’ve declared how a Taco and Order should be validated, we need to revisit each of the controllers, specifying that validation should be performed when the forms are POSTed to their respective handler methods.

When persisting objects to a database, it’s generally a good idea to have one field that uniquely identifies the object. Your Ingredient class already has an id field, but you need to add id fields to both Taco and Order.

Moreover, it might be useful to know when a Taco is created and when an Order is placed. You’ll also need to add a field to each object to capture the date and time that the objects are saved. The following listing shows the new id and createdAt fields needed in the Taco class.

Listing 3.3. Adding ID and timestamp fields to the Taco class
@Data
public class Taco {

  private Long id;

  private Date createdAt;

   ...

}

Because you use Lombok to automatically generate accessor methods at runtime, there’s no need to do anything more than declare the id and createdAt properties. They’ll have appropriate getter and setter methods as needed at runtime. Similar changes are required in the Order class, as shown here:

@Data
public class Order {

  private Long id;

  private Date placedAt;

  ...

}

Again, Lombok automatically generates the accessor methods, so these are the only changes required in Order. (If for some reason you choose not to use Lombok, you’ll need to write these methods yourself.)

The first thing you’ll notice about the code in listing 3.12 is that DesignTacoController is now annotated with @SessionAttributes("order") and that it has a new @ModelAttribute annotated method, order(). As with the taco() method, the @ModelAttribute annotation on order() ensures that an Order object will be created in the model. But unlike the Taco object in the session, you need the order to be present across multiple requests so that you can create multiple tacos and add them to the order. The class-level @SessionAttributes annotation specifies any model objects like the order attribute that should be kept in session and available across multiple requests.

The real processing of a taco design happens in the processDesign() method, which now accepts an Order object as a parameter, in addition to Taco and Errors objects. The Order parameter is annotated with @ModelAttribute to indicate that its value should come from the model and that Spring MVC shouldn’t attempt to bind request parameters to it.

After checking for validation errors, processDesign() uses the injected TacoRepository to save the taco. It then adds the Taco object to the Order that’s kept in the session.

In fact, the Order object remains in the session and isn’t saved to the database until the user completes and submits the order form. At that point, OrderController needs to call out to an implementation of OrderRepository to save the order. Let’s write that implementation.