CS636 Class 14

Handout: Pizza2 Sample Code

Finish Chap. 13 slides

Note doc JPA2Notes.html with comments on the book pages, some overlap with notes on the slides.

Eclipse Debugging Demo

Look at pizza2, just posted.

But use pizza1 for debugging demo, since like current project.
Break it by changing insertOrder to insertOrderx in PizzaOrderDAO
Run with "ant hsqlSysTest", see exact reason and where.

Break it by putting "order = null;" just before call to insertOrder, in StudentService.makeOrder.
Run with "ant hsqlSysTest", see backtrace and NullPointerException reported from insertOrder in DAO, not the location of the mistake.
Run with debugger in eclipse: right-click SystemTest.java in project explorer, select Debug>Java application
See crash report, same as running with ant, except link to PizzaOrderDAO.java, site of crash (line of code)
Set breakpoint in PizzaOrderDAO: click along left-hand edge of editor window at desired line.
Run again with debugger: can use little bug icon at top of Debug window.
See before-crashed state in insertOrder, with back trace in window, editor on PizzaOrderDAO.java showing breakpoint
Click levels in back trace, see editor windows on various files.
Click makeOrder level, see its code, obvious problem user = null, but we'll pretend we don't see it yet.
Set breakpoint in caller, makeOrder, to find the cause.
Run again with debugger, stops in makeOrder, use step-over button to advance line by line, seeing variable values change.
Finally see user become null, that's the bug.

Can also step-into: rerun with breakpoint in SystemTest, step into makeOrder, etc.

Note debugger perspective, different from Java perspective, switch back and forth with perspective buttons.
You don't have to live with what is shown in the Debugger perspective:
Example: add Project Explorer view to Debugger display.

Look at Pizza2 code online and on handout.

Pizza2 Sample Code:

We have to tell JPA about a domain object before it can help us with persistence

Look at PizzaOrder markup in handout

Class anotation @Entity and field annotation @Id are the minimal needs.

See also @Column and @GeneratedValue for id

Example service layer method: one transaction, where startTransaction creates a new em, and commit closes it.

So all those Toppings are detached objects when they get to the presentation code.

DbDAO Transaction methods

Shown at end of handout: config code in PizzaSystemConfig creates an emf,

Not shown: PizzaSystemConfig then passes the emf to DbDAO as a param to its constructor

So DbDAO has the emf, and uses it to create a new em for each transaction.

DbDAO CRUD methods

Still have CRD of CRUD, but don’t need Update methods in DAO.  Since the em tracks the managed domain objects, we can simply change their fields by calling their setters in the service layer. The em will be able to figure out that this field has been changed when it sends data to the database, usually at commit time.