title: JPA code that supports statless and stateful usages
body: <p>I have two applications that share a database.  One application that processes messages from a queue and another has JSF screens that support the exact same functions (plus many others) that the queue uses.  There is considerable overlap in the JPA code, such that I want to create a single reusable module for both applications (as it is when I make a change to a JPA query I have to apply it to both applications).</p>
  
  <p>Now we have a user id attached to every row on the database.  When the row is created in response to a queue message the user id is static "SYSTEM_X_ID" (for example).  But when the row was created by a user via a JSF screen it would be the user id in the session.  </p>
  
  <p>That's where my problem is - the queue version of the application doesn't have a session.  The session object is injected.  So how can I write reusable database code to check for the existence of an injected session object that would be injected in the JSF application but not in the queue application?  Is it even possible?</p>
  
  <p>One idea I have had so far is to modify the BaseDao class that all other Dao classes extend such that in the queue version it is statically assigned with no reference to the session object and in the JSF version inject the session object and use the user id in it.  I would prefer to not have to do that though - hence my question.</p>
  
  <p>Thanks in advance.</p>