title: JPA - multiple jars defining @Entity objects
body: <p>We are developing a web application with Spring, Hibernate and Maven in a very modular fashion. There are core projects defining data access and view specific stuff, then there are modules defining logic and entities (<code>@Entity</code>) and then there's the web app defining controller and view.</p>
  
  <p>Now we have a security module defining security entities like account and role and we have a prototype module defining some example entities like customer and order. Both have a <code>PersistenceUnit</code> defined inside a <code>persistence.xml</code> which is pretty much empty except for the PersistenceUnit name, as all database configuration is done in the web app with a datasource.xml. The web app is supposed to load both jars as maven dependencies.</p>
  
  <p>Both projects will build fine, autocscanning all entities and creating them for their respective unit tests. They will also get loaded inside the web app successfully if added individually. </p>
  
  <p>However, as soon as both are loaded at the same time, the second one will override the <code>PersistenceUnit</code> of the first one and thus create an <code>IllegalArgumentException : Not an entity</code> for all entities from the first one. If both projects have a different persistence unit, loading the web app will throw another exception saying that <code>no single default persistence unit</code> defined.</p>
  
  <p>So.. how can I get all <code>@Entity</code> annotated classes to load up in my web app without having to define them all inside the <code>persistence.xml</code> (like <a href="http://javathoughts.capesugarbird.com/2009/02/jpa-and-multiple-persistence-units.html" rel="noreferrer">here</a>) but rather via component scan?
  <a href="http://forum.springsource.org/showthread.php?61763-Problems-with-JPA-and-multiple-persistence-units" rel="noreferrer">This</a> seems like an idea, though I don't know how to use and test it...</p>
  
  <p>I think we either have to merge all PersistenceUnits inside the web app or load all Entities programmatically. Defining them hard-coded inside the persistence.xml is not an option for us.</p>