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
Also new to database directory: 3 versions of persistence.xml, one for each DB
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 in lib. Then you can edit the sources, use the debugger, etc., just as before.
Advanced Topic (not required Fall '16, rest of this file): To
use the JPA tools to create domain classes from a database schema, you
need to create an eclipse "JPA Project".
Select JPA Project instead of Java Project when you create the project. Specify the location as you did with Java Projects. Leave the defaults of <None> for target runtime and Minimal JPA 2.0 Configuration, etc. on the first window. Click Next to get to the Java window, which is the same as for a Java Project. Click Next to get to the JPA Facet window, and you should see Platform filled in with EclipseLink 2.1.x because of the jar file in lib, 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.
Then use the Build Path>Libraries window to Add Jars: everything in lib except the junit jar. Also Add Library> JUnit>, select JUnit4, Finish. Get HSQLDB going, load it up, use “ant config-hsqldb”, check with “and sysTest”.
The files are now right for HSQLDB, but eclipse will still be using the older files until you Refresh the project (right-click on project in Package Explorer). After project refresh, you should be able to run SystemTest as a Java Application. Note: ignore any warnings about "foo.jar will not be exported or published" We are not exporting or publishing...
Remember: you need to Refresh the project after changing files outside of eclipse!
Refreshing the project directs eclipse to reread all the files from the file system. It has its own cached data that it works from normally, even the .class and related files like persistence.xml. So switching from HSQLDB to Oracle when using eclipse to run things :
1. Do ant config-oradb
2. Refresh the project by right-clicking it in Package Explorer and selecting Refresh or using the Project menu bar item.
3. Now run stuff in eclipseTo 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 HSQLDB 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 has a live connection to a
database with a schema that matches what it expects from the
annotations. Get HSQLDB going, load it up, and select the project in
PackageExplorer, right-click and Validate. In Project Properties>JPA,
select the HSQLDB Connection, and if it shows SA as the catalog,
override it with PUBLIC. If that doesn't work (still shows red stars),
Note: MySQL is non-compliant with something related to case-sensitivity that the eclipse Eclipselink UI depends on, so use HSQLDB or Oracle for eclipse development. The runtime support works fine for MySQL.