title: Making sense of JPA and EJB3 and WebServices
body: <p>I'm just getting started with JPA, creating stateless session beans from the JPA entities, then accessing the beans through a web service.  While I have a lot of experience developing database backed web services "the old way", I have some questions about what's going on behind the scenes with this new "annotation driven approach".</p>
  
  <p>What I see is, NetBeans sort of directs you to build applications in this manner though their wizards.</p>
  
  <ol>
  <li>Create an Enterprise Application with EJB and Web Application modules.</li>
  <li>Create Entity classes.  I created mine from an existing database.</li>
  <li>Create session beans from the entity class.</li>
  <li>Create Web services from the session bean.</li>
  </ol>
  
  <p>It all looks pretty simple, but what's going on behind the scenes?  Specifically:</p>
  
  <ol>
  <li>I see that the web service (created with the <code>@WebService</code> annotation) references my stateless session bean (using the <code>@EJB</code> reference).  
  
  <ul>
  <li><strike>What happens here?  Does it just grab an instance of the EJB from the application server's EJB pool?</strike><br>
  <em>Nevermind.  I was thinking of an instance where there was more than 1 table - meaning more than 1 type of Entity class and more than 1 type of EJB.  I was looking at the web service and just seeing the <code>@EJB</code> reference and didn't understand who it was getting the bean type from that annotation.  Just below that however, it the reference to the local bean interface - so that's that.</em></li>
  <li>If there is more than 1 type of EJB deployed to the server, how does it know which one to grab?</li>
  </ul></li>
  <li>The EJB is defined via the <code>@Stateless</code> and <code>@Local</code> annotations. The bean implementation references an <code>EnityManager</code> via the <code>@PersistenceContext</code> annotation.  
  
  <ul>
  <li>Where is the jndi lookup to the database done (maybe in the <code>persistence.xml</code> file)?  </li>
  <li>Do all of the EJBs share a common <code>EntityManager</code> (assuming the <code>EntityManager</code> is thread safe)?  If not, I know that the <code>EnityManager</code> utilizes a second level cache to help reduce trips to the database, are these caches somehow kept in sync?</li>
  </ul></li>
  </ol>
  
  <p>I know this is a lot of questions, but they're all sort of related and there seem to be a lot of complicated concepts for something that's so easy to build through the wizards.  I want to make sure I understand what's all going on here.</p>
  
  <p>Thanks in advance!</p>