1

In my java EE application , I wrote a simple bean that I referenced container managed entity manager like below

@Stateless
public class MessageService {

@PersistenceContext
private  EntityManager entityManager;

But even if I have only java EE 8 and H2 database dependencies on my classpath, I was able to reference and use EntityManager from my bean. Should not I have been have to have a JPA implementation on my classpath like Hibernate etc.?

  • 1
    Are you using an Application Server (JBOSS, Wildly, GlassFish, etc) to deploy your application? – Carlitos Way Aug 15 '22 at 05:16
  • Yeah . I am using latest Wildfly server to deploy my application. How do I know if required libs are contained in my deployed application? –  Aug 15 '22 at 09:54
  • Do you have a persistence.xml file? – Carlitos Way Aug 15 '22 at 16:11
  • I added persistence.xml file. But didn't supply any provider tag in the xml file. How does application server know which vendor to use for jpa implementation? –  Aug 16 '22 at 09:20
  • 1
    Related: https://stackoverflow.com/q/7295096 – BalusC Aug 16 '22 at 19:10

1 Answers1

0

Well, based on your comments:

I am using latest Wildfly server ...

And

I added persistence.xml file

The answer is: Wildfly will include for you some libraries in the Application's classpath when some conditions are met ...

According to the documentation, in your case, it adds JPA subsystem and its implementation (Hibernate) when it detects either the use of @PersistenceUnit or @PersistenceContext annotation or other XMLs tags in some deployment descriptors ...

You can read more about this in:

Wildfly Automatic Dependencies

Implicit module dependencies for deployments

NOTE: The most recent documentation that I found was Wildfly 26 ... however, this is something that they've done since Wildfly 8 ...

Carlitos Way
  • 3,279
  • 20
  • 30