Read Chapter 13 of Murach skipping Netbeans coverage.
1. Pg. 429: We will not use GenerationType.AUTO. This would be OK if we only used mysql’s auto-increment columns, as expected in the text. Oracle does not support auto-increment, so we need a portable solution. We will use GenerationType.TABLE instead. See pizza2 code.
2. Pg. 431: We will use field annotations.
3. Pg. 437, 439: We will create the EntityManagerFactory in the system-config code, and the EntityManage in the DAO code..
4. Pg. 441 shows an example of JPQL, the JPA way to code SQL in the DAO. These Invoice objects retrieved will be set up to ensure that their Lineitems are available if accessed in the service layer, so this code is much simpler than the corresponding code in music1.
5. Pg. 445: We will not use em.merge(user); in our DAO code. This call is meant to handle cases where presentation changes are allowed to be sent to the database, against our coding rules for enterprise web apps. Not all JPA uses are such apps. More on this later.
6. Pg. 449: Note the static methods. We will use a singleton DAO object with instance methods. Also, note that this looks like DAO code, but also starts and finishes the transaction. We will start and commit transactions in the service layer, not the DAO layer. So our DAO code will just have the database actions. Further, we would not need the “update” method listed here, because changes we make to the POJOs in the service layer are tracked and the corresponding updates are done in the database at commit (sometimes earlier), without any further em method calls (just the em.commit()).
Notes on setting up the pizza2 JPA Project
Need to reload the databases for pizza2, because there’s a new table
See pizza2/README for how to run the project
Creating a Project in eclipse for non-web project using JPA, such as pizza2.
For our purposes, you can treat a JPA project as a Java project, since all the needed libraries are specified by Maven. Then you can edit the sources, use the debugger, etc., just as before.
Advanced Topic (FYI): To use the JPA tools to create domain classes from a database schema, you need to create an eclipse project with a "JPA Facet". Create the project from the filesystem as usual, then right-click the project in Project Explorer, select Properties>Project Facets, and click JPA. Then you should see JPA as a new project property. In the JPA area of project Properties, leave the defaults of Generic 2.1 for Platform, and JPA implementation saying "Disable Library Configuration", which just means we will do it ourselves. Leave these two entries alone. The Connection should say <None>. Leave it that way for now. You need to connect just when you want to use the tools, since H2 allows only one connection at a time.(or use Oracle or mysql for the JPA connection). Make sure to load H2 with the tables before trying the tools.
To actually use the JPA tools, you also need to specify a Connection in the JPA Properties. Pull down the list next to <None> and select your H2 or whatever connection, hopefully previously set up in the Data Source Explorer. If nothing shows, select the "add connection" link and fill one in. Leave the rest of the entries as is and click Finish.At this point you still may see red stars on your domain classes,
because the eclipse JPA UI support isn't happy (if a Connection is
specified in JPA Properties) unless it knows it has a live connection to
a database with a schema that matches what it expects from the
annotations. Select the project in PackageExplorer, right-click and
Validate to have it check again.
Once the domain classes are red-star-free, try out the tools. Right-click the project, select JPA Tools>Generate Entities from Tables, and you will be using a wizard to guide the formation of the entity classes.