Showing posts with label Oracle TopLink Essentials JPA Session. Show all posts
Showing posts with label Oracle TopLink Essentials JPA Session. Show all posts

Wednesday, January 17, 2007

Accessing the Session in TopLink Essentials

Although TopLink Essentials is the reference implementation for JPA, it was derived from Oracle TopLink and therefore shares the same core concepts and classes. Recently I was writing some (admittedly bizarre) JPA code and wanted to verify what was in the shared cache. Of course there's no way to look in the TopLink shared cache using JPA so I needed to use the TopLink API. Fortuately, it's still there if you need it.

To obtain a reference to a session in TopLink Essentials(as in Oracle TopLink) you use the SessionManager. Once you have the session you need, you can use it as you would in Oracle TopLink:


Session session = SessionManager.getManager().getSession("library");
List books = session.readAllObjects(BookImpl.class);
assertEquals("num books", 1, books.size());


The "library" session is the TopLink object underlying the JPA "library" persistence unit as defined in my persistence.xml. But, to make the library session available through the SessionManager you have to add a property to your persistence unit:


<?xml version="1.0" encoding="UTF-8"?>
<persistence>
  <persistence-unit name="library">
    <properties>
      <property name="toplink.session-name" value="library">
...


By adding this declaration you're effectively publishing your "library" persistence unit under a session name of "library". You could publish your persistence unit under a different session name but why confuse matters.