background image

Setting Entity Relationships

<< Temporal Types in the order Application | Building the order Application >>
<< Temporal Types in the order Application | Building the order Application >>

Setting Entity Relationships

Creating Entities
The RequestBean.createPart business method creates a new Part entity. The
EntityManager.persist
method is used to persist the newly created entity to the database.
Part part = new Part(partNumber,
revision,
description,
revisionDate,
specification,
drawing);
em.persist(part);
Finding Entities
The RequestBean.getOrderPrice business method returns the price of a given order, based on
the orderId. The EntityManager.find method is used to retrieve the entity from the database.
Order order = em.find(Order.class, orderId);
The first argument of EntityManager.find is the entity class, and the second is the primary
key.
Setting Entity Relationships
The RequestBean.createVendorPart business method creates a VendorPart associated with a
particular Vendor. The EntityManager.persist method is used to persist the newly created
VendorPart
entity to the database, and the VendorPart.setVendor and
Vendor.setVendorPart
methods are used to associate the VendorPart with the Vendor.
PartKey pkey = new PartKey();
pkey.partNumber = partNumber;
pkey.revision = revision;
Part part = em.find(Part.class, pkey);
VendorPart vendorPart = new VendorPart(description, price,
part);
em.persist(vendorPart);
Vendor vendor = em.find(Vendor.class, vendorId);
vendor.addVendorPart(vendorPart);
vendorPart.setVendor(vendor);
Using Queries
The RequestBean.adjustOrderDiscount business method updates the discount applied to all
orders. It uses the findAllOrders named query, defined in Order:
The order Application
The Java EE 5 Tutorial · September 2007
720