14

I'm trying to configure and Maven multi-module project with Spring / JPA. Here's the general layout. I have a root module with 5 children modules.

backoffice (root maven module)
|
-(maven module)-----core (this is where persistence.xml and entityManager stuff resides).
|
-(maven module)-----employee (employee related entities, controllers, etc.)
|
-(maven module)-----vendor (vendor related entities, controllers, etc.)
|
-(maven module)-----customer (customer related entities, controllers, etc.)
|
-(maven module)-----web (contains all the web stuff).

I have all the jpa stuff in core/src/main/resources/META-INF (persistence.xml, spring-context w/ EntityManagerFactory, dataSource, etc.). The idea is I want to share the persistence stuff across all sub-modules (employee, vendor and customer).

The problem is that when the web app starts up, it can't find the EntityMangerFactory. If I setup the JPA stuff in each sub module (employee, vendor and customer), then it works.

How do I setup all my persistence related stuff in core and then share it across the other modules?

Thanks in advance.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
user523078
  • 227
  • 3
  • 7
  • 1
    Have you added a dependency to core to the employee, vendor and customer modules? Honestly not sure I'd make them as separate modules even – willcodejavaforfood Nov 28 '10 at 18:14
  • Okay the I forgot to add the context file from core to my web.xml. Now that I've done that the webapp starts up, but It can't find the entities in the other modules. – user523078 Nov 28 '10 at 19:02
  • What do you mean by: " but It can't find the entities in the other modules"? – Ralph Nov 29 '10 at 17:16
  • Please Post you persistence.xml – Ralph Nov 29 '10 at 17:43
  • When the persistence.xml is in the core module, It doesn't see the entities in the employee, vendor or customer module. It looks like I need a persistence.xml in each maven module. That kind of stinks, because then I have to override the persistenceUnitManager and merge the persistence.xml files. – user523078 Nov 29 '10 at 17:51

2 Answers2

4

Well, here is the solution that I ended up using in case anyone is interested.

http://labs.bsb.com/2010/11/configuring-modular-jpa-applications-with-spring/

Some of the responders asked why would I want to setup the project this way in the first place. Well the reason is modularity. I want everything in small testable and deploy-able units. Ultimately we will weave OSGI into the mix so the customer can choose which modules they want to install. Each module must have the ability to run on its own.

user523078
  • 227
  • 3
  • 7
-2

If the employee, vendor and customer entities and controllers are so logically separate as to need them in separate modules, then I would say that clearly they should have their own entityManager.

If you want to have a single webapp, and a single entitymanager, then why do you want to have separate modules for each chunk of entities? Just stick employees, vendors and customer in core, with the entity manager, and be done.

Or, give each one their own entitymanager, (and probably each one their own webapp)

Karl P
  • 11
  • this does not answer the question at all, it's not what it's expected in SO. Don't question the reason of the question if you don't know the anser. – Panthro Jan 25 '14 at 00:55