12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 4 ■ MANAGING PERSISTENT OBJECTSobject and persist it. Because Customer is cacheable (see Listing 4-23), it should be in the second-levelcache (by using the EntityManagerFactory.getCache().contains() method). Invoking thecache.evict(Customer.class) method removes the entity from the cache.Listing 4-24. The Customer Entity is CacheableCustomer customer = new Customer("Antony", "Balla", "tballa@mail.com");tx.begin();em.persist(customer);tx.commit();// Uses the EntityManagerFactory to get the CacheCache cache = emf.getCache();// Customer should be in the cacheassertTrue(cache.contains(Customer.class, customer.getId()));// Removes the Customer entity from the cachecache.evict(Customer.class);// Customer should not be in the cache anymoreassertFalse(cache.contains(Customer.class, customer.getId()));JPQLYou just saw how to manipulate entities individually <strong>with</strong> the EntityManager API. You know how to findan entity by ID, remove it, update its attributes, and so on. But finding an entity by ID is quite limiting, asyou only retrieve a single entity using its unique identifier. In practice, you may need to retrieve an entityby criteria other than the ID (by name, ISBN, etc.) or retrieve a set of entities based on different criteria(e.g., all customers living in the USA). This possibility is inherent to relational databases, and JPA has alanguage that allows this interaction: JPQL.JPQL is used to define searches against persistent entities independent of the underlying database.JPQL is a query language that takes its roots in the syntax of Standard Query Language (SQL), which isthe standard language for database interrogation. But the main difference is that in SQL the resultsobtained are in the form of rows and columns (tables), whereas JPQL uses an entity or a collection ofentities. JPQL syntax is object oriented and therefore more easily understood by developers whoseexperience is limited to object-oriented languages. Developers manage their entity domain model, not atable structure, by using the dot notation (e.g., myClass.myAttribute).Under the hood, JPQL uses the mechanism of mapping to transform a JPQL query into languagecomprehensible by an SQL database. The query is executed on the underlying database <strong>with</strong> SQL andJDBC calls, and then entity instances have their attributes set and are returned to the application—all ina very simple and powerful manner using a rich query syntax.The simplest JPQL query selects all the instances of a single entity:SELECT bFROM Book b145

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!