Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

CHAPTER 28: Java Persistence API > Saving and Loading Objects

Saving and Loading Objects

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.)


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial