Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inject EntityManager into class inside lib of a war  RSS feed

 
Marcus Schulz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I had a working webapp where all layers has been mixed inside one project.
My task was to split this into several projects because I want to reuse common classes and modules. So far good idea.
I use maven, eclipse with m2-eclipse and wildfly 9

It resulted into the following structure:

base-entities-jpa (common entities such as user and so on)
base-controller (service classes and data access layer)
base-webapp (some utilities for JSF, Webfilter...)

Every application should base on these projects.

So I have

app1-model-jpa (additional entities for the specialized webapp)
app1-controller (additional controller classes)
app1-webapp (war file with JSF components)

My current problem is that I am not able to inject the EntityManager into a class that is inside base-controller.
I placed An "AbstractDAO" there with a member variable for the EntityManager.
Classes, that inherit the AbstractDAO are annotated as @Stateless

The projects base-* are located as jar into WEB-INF/lib of app1-webapp.

I tried two days, with ear packaging, without, with CDI Producers and with @PersistencContext.
The entity manager member still stays null.

Any hint what I should do is really welcome..
Or is it just impossible? Cannot believe ..

Thanks in advance

Marcus
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Did you package the JAR file as an EJB-JAR file? Those can be included in WARs without a problem.
2) Did you use @Inject, @Resource or @PersistenceContext? You should use the last one for EntityManagers.
 
Marcus Schulz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

yes, the packaging is as follows:

base-entities-jpa (jar)
base-controller (ejb)
base-webapp (jar)


app1-model-jpa (jar)
app1-controller (ejb)
app1-webapp (war)

 
Marcus Schulz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a very stupid mistake... I instantiated a bean with "new constructor" :/

Now my entity manager is injected as appreciated.

I changed the project structure to ear meanwhile.
I put the persistence.xml in ear/META-INF and the base-entity-jpa.jar is located in ear/base-entity-jpa.jar

I now have the problem that the entities in the jar file aren't found from persistence context.
I added <jar-file>lib/base-entity-jpa.jar</jar-file> to the persistence.xml

When I take a look what eclipse deployed to wildfly, I see that that my base-entity-jpa.jar file is a folder named base-entity-jpa.jar instead of a regular jar file.
mvn clean package creates a jar.

Any ideas?

Well, I investigated any further and the behaviour becomes quite strange.

When I add a breakpoint where the entitynotfound exception ist thrown and switch to display view and type

I get a resultlist as expected.

The same query with Criteria API throws an exception.



No ideas so far. Is it a bug?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!