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-unit name="library">
      <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.

Monday, January 15, 2007

You have to start sometime...

Like many before me here I am posting my first blog entry. This day had to come eventually ;-).

I'm going to be blogging on TopLink related topics--both Oracle TopLink and the open source TopLink Essentials--as well as on the Java Persistence API (JPA) in general. I'll also share news about the Eclipse Dali JPA Tools project which I'm involved with. Dali's heading for 1.0 in June as part of the Eclipse Europa release so there's lots of work going on there.

I'd created this account a few days ago and was looking for a topic for my first blog but before I found it I read an interesting blog by Mike Milinkovich on the recent announcement of people named ACM Distinguished Engineers and thought I'd add a comment. And when I did, it linked my comments to my blog--and it was as yet blank. So here I am. Hopefully subsequent entries will be more substantial.