• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inject EntityManager into class inside lib of a war

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic