Tutorial+2+-+The+Queries

Queries
In these example, the variable "em" is an entity manger initialized just like it was in the first tutorial.

Setup: None required >
 * Empty String**

Setup: None required >
 * Unknown Class**

Setup: None required >
 * Minimal "Get All"**

Setup: Insert exactly 1 Person entity in the database >
 * Successfully Get a Single Object**

Setup: Make sure there are no Person entities in the database >
 * Unsuccessfully Try to get a Single Object When There Are None**

Setup: Insert two or more Person entities in the database >
 * Unsuccessfully Try to get a Single Object With Too Many**

Setup: Insert a Person in the database, make sure to get the key of the object inserted >
 * Find By Primary Key**

Setup: None required >
 * Unsuccessfully Find by Primary Key**

Setup: Insert one person record where the Person's first name = "Brett". code format="java5" final List<?> list = em.createQuery("from Person where p.firstName = ?1") .setParameter(1, "Brett").getResultList; code
 * Search Using Query Parameter and Storing Result as List<?>**

Setup: Insert one person record where the Person's first name = "Brett". code format="java5" final List list = em.createQuery(       "from Person where firstName = ?1").setParameter(1, "Brett") .getResultList; code
 * Search Using Query Parameter and Storing Result as List**

Setup: Insert one person record and store the primary key. Also make sure the first name of the person equals "Brett". code format="java5" final Person pByKey = em.find(Person.class, personKey);
 * Do Find by Primary Key and Queries Return == Objects**

final Person pByWhere = (Person) em.createQuery(       "SELECT p from Person p where firstName='Brett'") .getSingleResult; code

Setup: None required >
 * Use Wrong Class Name**

Setup: None required >
 * Use Wrong Field Name**

Setup: None required, but maybe insert a single person whose first name = "Brett". >
 * Use Column Name Instead of Field Name**

Setup: None required >
 * Use a Parameter but Provide Wrong Index**

Setup: None required >
 * Set Parameter Where There are None: Version 1**

Setup: None required >
 * Set Parameter When There Are None: Version 2**