Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Finally, it remains to be clarified how to access your objects over an EntityManager. Take the class DataModel, in which you implemented the interaction with the native Hibernate interface, again. You want to substitute it now with the JPA. Let's take a look at the methods getAlbums() and getGenres(), for example. As usual, you first create a transaction in which you want to execute your query or action. With the method getTransaction() you get an instance of an EntityTransaction by the EntityManager. Start the transaction with begin() and then create a Query instance for the JPQL query SELECT a FROM Album a. This query provides all objects of the table Album. You get this result in the form of a List by the getResultList() method. Finish the transaction successfully with commit(). (See Listing 28-5.)