* @param args Cmd line arguments
*/
public static void main(String[] args) {
EntityManagerFactory emFactory = null;
EntityManager em = null;
Product prod = null;
CriteriaBuilder criteriaBuilder = null;
CriteriaQuery<Product> criteriaQuery = null;
Root<Product> product = null;
List<Product> products = null;
try {
//Create a EntityManager instance using EntityManagerFactory and a CriteriaBuilder
emFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
em = emFactory.createEntityManager();
criteriaBuilder = em.getCriteriaBuilder();
//Select Products using a parameter on name
System.out.println("*** SELECT PRODUCTS USING A PARAMETER ON NAME");
criteriaQuery = criteriaBuilder.createQuery(Product.class);
product = criteriaQuery.from(Product.class);
criteriaQuery.select(product).where(criteriaBuilder.equal(product.get("name"), "PS3"));
try {
prod = em.createQuery(criteriaQuery).getSingleResult();
System.out.printf("[%s] : %s, %s\n", prod.getId(), prod.getName(), prod.getDescription());
} catch (NoResultException nre) {
//Exception throw by "getSingleResult()" if no result is returned by the query
System.out.println("No product found !");
} catch (NonUniqueResultException nure) {
//Exception throw by "getSingleResult()" if several record is found by the query