5

I have an application which uses JPA/Hibernate and Google Guice. Guice is bootstrapped in a ServletContextListener and it sets up the EntityManagerFactory itself.

The application works fine on Tomcat 7, but when I deploy to JBoss AS7 it fails because JBoss decides to automatically setup JPA prior to invoking my ServletContextListener.

How can I get JBoss to not initialize JPA automatically and instead wait for my ServletContextListener to do it?

Update

According to the link that James provided below:

During application deployment, JPA use is detected (e.g. persistence.xml or @PersistenceContext/Unit annotations) and injects Hibernate dependencies into the application deployment.

https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-Introduction

I need to figure out how to disable this "auto-detect" feature.

Update #2

Container management of JPA can be disabled by adding the following property to the persistence.xml:

<property name="jboss.as.jpa.managed" value="false" />

According to this topic, as of February 2012 this functionality is only available in a nightly build.

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • This is quite old, but I encountered the same problem. My extra problem is that I am not using persistence.xml but rather declaring the entity manager factory with java code and it seems that adding the property there is ignored and makes no difference. Any idea if it can be solved with this approach? – andreadi Apr 24 '14 at 10:43
  • @andreadi No idea. I was only testing an application out on AS7 but never moved forward with using it for that project. You're best bet will probably be to ask a new question with your specifics. – Steven Benitez Apr 24 '14 at 14:20

1 Answers1

2

JBoss AS7 is a full Java EE server. That means that a JPA implementation comes bundled with it. In Tomcat you have to provide your own JPA implementation and are essentially running JPA like you would in Java SE.

I would recommend you read the JPA reference documentation for AS7.

You also get CDI so there is no real need for Guice. You could probably use Guice instead of CDI, but honestly I couldn't tell you how :-)

James R. Perkins
  • 16,800
  • 44
  • 60
  • I've previously used Oracle WebLogic 11g and it doesn't forcefully load JPA for you by default, so it seems strange that JBoss would. I'll check out the link though. – Steven Benitez Apr 19 '12 at 16:21
  • It's probably seeing your persistence.xml and loading Hibernate and the JPA API. I'm pretty sure most EE servers do this, but I can't guarantee that. I did a bit of searching and nothing really popped out at me on how to disable it. You might be able to disable the JPA subsystem and bundle Hibernate with your application. – James R. Perkins Apr 19 '12 at 16:26
  • I haven't found any way to disable this feature. I've upvoted your answer and I'll accept it early next week when I'm back in the office if no one else provides information on how to disable the auto-detection of JPA. I appreciate your assistance on this, your link at least explained what was happening. – Steven Benitez Apr 20 '12 at 20:18
  • 1
    What about adding a `` to your `persistence.xml`? I'm not sure if it will work the way you want, but it might. This is what it says on that page; "can be set to false to disable container managed JPA access to the persistence unit. The default is true, which enables container managed JPA access to the persistence unit. This is typically set to false for Seam 2.x + Spring applications." – James R. Perkins Apr 20 '12 at 20:47
  • Holy smokes. I skimmed right over that. I'll try that on Monday morning. This looks like exactly what I needed. I'll post back then. Thanks! – Steven Benitez Apr 21 '12 at 18:49
  • The version of AS7 that I am using doesn't yet have the property override feature (see my revised answer for details), however, I am convinced from what I have read about this property that it is the fix and what I am looking for. Thanks again for your help, James. – Steven Benitez Apr 23 '12 at 13:09