# Pro JPA 2: Mastering the Java Persistence API

# Examples

### Java SE Examples

The SE examples (e.g. Chapter 2) do not require a running application server, only the db.

### Java EE Examples

The other examples require a running application server. Go to the 'bin' folder and start the Glassfish application server using the batch file (startservers.bat). Once started, go to the desired chapter's example folder and build/deploy the application using the deploy.bat batch file. Once that is done, come back to this page and click on the link of the desired example. This will provide you with a web client to run the example.

- Chapter 2: Getting Started
  - These examples demonstrate the Java Persistence API in an SE environment. To run, go to the Chapter2 folder and run the batch file for the example. It provides both a simple interactive client to create/list/remove employees and a basic test like the one found in the chapter.
- Chapter 3: Enterprise Applications
  - 1.SLSB Example
  - 2.SLSB Lifecycle Example
  - 3.SLSB Remote Example
  - 3b.The SLSB Remote example also has a fat client that can be run by going to the example's folder (Chapter3/03-slsbRemoteExample) and running the runClient.bat batch file.
  - 4.SFSB Example
  - 5.SFSB Lifecycle Example
  - 6.Singleton Session Bean Example
  - 7.MDB Example
  - 8.Servlet Example
  - 9.Dependency Lookup Example (using InitialContext)
  - 10.Dependency Lookup Example (using EJBContext)
  - 11.Field Injection Example
  - 12.Setter Injection Example
  - Note: Injection of other EntityManager and EntityManagerFactory instances is shown in example 16 and throughout the examples in subsequent chapters. Injection of a SessionConext can be seen in example 9.
  - 13.Bean Managed Transaction (BMT) Demarcation Example
  - 14.Container Managed Transaction (CMT) Demarcation Example
  - 15.UserTransaction Example
  - 16.Complete EmployeeService Example
- Chapter 4: Object-Relational Mapping
  - 1.Field Access Example
  - 2.Property Access Example
  - 3.Mixed @Access Example
  - 4.Custom Table/Schema Example
  Basic Mappings
  - 5.Custom Column Mapping Example
  - 6.LOB Mapping Example
  - 7.Enum Mapping Example
  - 8.Temporal Mapping Example
  - 9.Transient Mapping Example
  Id Generation
  - 10.Auto Id Generation Example
  - 11.Table Id Generation Example
  - 12.Sequence Id Generation Example (The Derby database does not support this feature)
  - 13.DB Identity Id Generation Example
  Relationship Mappings
  - 14.Many-to-One Example
  - 15.Many-to-One Join Column Example
  - 16.One-to-One Unidirectional Example
  - 17.One-to-One Bidirectional Example
  - 18.One-to-One PK Mapping Example
  - 19.One-to-Many Bidirectional Example
  - 20.One-to-Many Target Entity Example
  - 21.Many-to-Many Bidirectional Example
  - 22.Many-to-Many Join Table Example
  - 23.One-to-Many Unidirectional Example
  - 24.One-to-One Lazy Fetching Example
  - 25.Using Embedded Objects Example
  - 26.Sharing Embedded Objects Example
- Chapter 5: Collection Mapping
  - 1.ElementCollection Example
  - 2.ElementCollection Override Table Column Example
  - 3.OneToMany List Example
  - 4.Persistently Ordered List Example
  - 5.ElementCollection Map Keyed on Strings Example
  - 6.ElementCollection Map Keyed on Enumeration Example
  - 7.OneToMany Map Example
  - 8.ManyToMany Map Example
  - 9.OneToMany Map Keyed on Entity Attribute Example
  - 10.Embeddable Key Mapping Example
  - 11.ManyToMany Embeddable Key override Example
  - 12.ElementCollection Map keyed on Entity Example
  - 13.Untyped Maps Example
- Chapter 6: Entity Manager
  - 1.Tx-Scoped Entity Manager Example
  - 2.Extended Entity Manager Example 1
  - 3.Extended Entity Manager Example 2
  - 4.Extended Entity Manager Example 3 (correct usage)
  - 5.Application Managed EntityManager in SE Example (run from folder)
  - 6.Application Managed Entity Manager in EE Example
  - 7.Transaction Scoped Persistence Context Example
  - 8.Extended Persistence Context Example
  - 9.Extended Persistence Context Collision Example
  - 10.Extended Persistence Context Inheritance Example
  - 11.Application Managed EntityManager with JTA Example
  - 12.Sharing Application Managed EntityManagers Example
  - 13.Resource-Local Transaction in SE Example (run from folder)
  - 14.Resource-Local Transaction in EE Example
  - 15.Persisting Entities with Relationships Example
  - 16.Persisting Entities with Relationships (using getReference) Example
  - 17.Removing Entities with Relationships Example
  - 18.Persisting Entities with Relationships using Cascade Example
  - 19.Removing Entities with Relationships using Cascade Example
  - 20.Merging Detached Entities Example
  - 21.Triggering Lazy Loading before Detaching Entities Example
  - 22.Using Eager Loading for Detaching Entities Example
  - 23.Avoiding Detaching Entities by using a Single Transaction Example
  - 24.Avoiding Detaching Entities by using an Application Managed EntityManager Example
  - 25.Avoiding Detaching Entities by using a SFSB and an extended EntityManager Example
  - 26.Using a SFSB and an Extended EntityManager to avoid merging Entities Example
- Chapter 7: Using Queries
  - 1.Dynamic Query Example
  - 2.Named Query Example
  - 3.Parameter Types Example
  - 4.Executing Queries Example
  - 5.Query Results Example
  - 6.Query Paging Example
  - 7.Message Board Example
  - 8.Bulk Query Examples
  - 9.Query Hint Examples
- Chapter 8: Query Language
  - JPQL Examples
- Chapter 9: Criteria API
  - 1.Criteria Search
  - 2.Criteria Search using Predicate Conjunction
  - 3.Criteria Search using Subquery
  - 4.Metamodel Strongly Typed API
  - 5.Canonical Metamodel Generation - (run generate.bat from the example folder)
  - 6.Criteria Search using Strongly Typed Canonical Metamodel