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 OBJECTSIf you know SQL, this should look familiar to you. Instead of selecting from a table, JPQL selectsentities, here Book. The FROM clause is also used to give an alias to the entity: b is an alias for Book. TheSELECT clause of the query indicates that the result type of the query is the b entity (the Book). Executingthis statement will result in a list of zero or more Book instances.To restrict the result, add search criteria where you can use the WHERE clause as follows:SELECT bFROM Book bWHERE b.title = 'H2G2'The alias is used to navigate across entity attributes through the dot operator. Since the Book entityhas a persistent attribute named title of type String, b.title refers to the title attribute of the Bookentity. Executing this statement will result in a list of zero or more Book instances that have a title equalto H2G2.The simplest select query consists of two mandatory parts: the SELECT and the FROM clause. SELECTdefines the format of the query results. The FROM clause defines the entity or entities from which theresults will be obtained, and optional WHERE, ORDER BY, GROUP BY, and HAVING clauses can be used torestrict or order the result of a query. The select syntax is defined in Listing 4-25 in the next section.There are also DELETE and UPDATE statements that can be used to perform delete and updateoperations across multiple instances of a specific entity class.SelectThe SELECT clause follows the path expressions syntax and results in one of the following forms: anentity, an entity attribute, a constructor expression, an aggregate function, or some sequence of these.Path expressions are the building blocks of queries and are used to navigate on entity attributes or acrossentity relationships (or a collection of entities) via the dot (.) navigation. Listing 4-25 defines the SELECTstatement syntax.Listing 4-25. SELECT Statement SyntaxSELECT FROM [WHERE ][ORDER BY ][GROUP BY ][HAVING ]A simple SELECT returns an entity. For example, if a Customer entity has an alias called c, SELECT cwill return an entity or a list of entities.SELECT cFROM Customer cBut a SELECT clause can also return attributes. If the Customer entity has a first name, SELECTc.firstName will return a String or a collection of Strings <strong>with</strong> the first names.SELECT c.firstNameFROM Customer c146

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

Saved successfully!

Ooh no, something went wrong!