Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 47 enterprise java Root Pool from cq from Pool class Then use the Root to create a TypedQuery object To do that call the EntityManager createQuery method and pass the CriteriaQuery select method along with the Root object TypedQuery Pool typedQuery em createQuery cq select from Lastly retrieve the results by calling the TypedQuery getResultList method typedQuery getResultList Once the query has been executed and results are returned any fields from the specified entity can be navigated because they are all available in the object Similarly any related records are made available For instance Listing 2 shows how to use a Pool The Pool entity contains a Collection Customer field that can be used for retrieving Customer objects that own a Pool of the specified type In the entity class the fetch mode is set to FetchType LAZY meaning that the Collection Customer is available only when the getCustomers method is explicitly called upon If I wanted to have the Collection retrieved at the same time as the Pool objects the fetch mode should be set to FetchType EAGER Listing 2 @ Entity @ Table name POOL @ XmlRootElement @ NamedQueries @ NamedQuery name Pool findAll query SELECT p FROM Pool p public class Pool implements Serializable private static final long serialVersionUID 1L @ Id @ Basic optional false @ NotNull @ Column name ID private Integer id @ Size max 10 @ Column name STYLE private String style @ Size max 10 @ Column name SHAPE private String shape @ Max value @ Min value if you know the range of your decimal fields consider using these annotations to enforce field validation @ Column name LENGTH private Double length @ Column name WIDTH private Double width @ Column name RADIUS private Double radius @ Column name GALLONS private Double gallons @ Column name SHALLOW_ DEPTH private Double shallowDepth @ Column name DEEP_ DEPTH private Double deepDepth @ OneToMany mappedBy pool fetch FetchType LAZY private Collection Customer customer public Pool
You must have JavaScript enabled to view digital editions.