Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:04:20+00:00 2026-05-14T05:04:20+00:00

It is continues of question ( struts 2 bean is not created ) I’m

  • 0

It is continues of question ( struts 2 bean is not created )
I’m using struts2 + toplink in my very simple web application under Tomcat.
On the page I would like use iteration tag. That is why I’ve declared some factory (SomeFactory) that resolves collection of entities (Entity).
Per article: http://download-uk.oracle.com/docs/cd/B32110_01/web.1013/b28221/usclient005.htm#CIHCEHHG
the only thing I need is declaration:

@PersistenceContext(unitName="name_in_persistence_xml")
public class SomeFactory
{
    @PersistenceUnit(unitName="name_in_persistence_xml")
    EntityManagerFactory emf;

    public EntityManager getEntityManager() {
       assert(emf != null); //HERE every time it is null
       return emf.createEntityManager();
    }
    public Collection<Entity> getAll()
    {
       return getEntityManager().createNamedQuery("Entity.findAll").getResultList();
}
}

What is wrong? May be i miss something in web.xml? How to pre-init toplink for web application to allow injection happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:04:20+00:00Added an answer on May 14, 2026 at 5:04 am

    You won’t get anything injected by Tomcat which is not a Java EE container (and even with a Java EE 5 container, injection only works for managed components like servlets, filters, listeners, EJB, web service endpoints…). So you will have to create the EntityManagerFactory manually (typically in a servlet or a helper class) and get the EntityManager from it:

    EntityManagerFactory emf  = Persistence.createEntityManagerFactory(PU_NAME);
    EntityManager entityManager = emf.createEntityManager();
    

    Note that creating an EntityManagerFactory is a costly operation and should not be done for each request. However, creating an EntityManager is not and you should get one for each thread. But in your case, I’d suggest to use the struts2-persistenceplugin to handle this.

    Thanks, but it seems […] that Java EE is not mandatory to use injection […] the Spring brings necessary engine for it.

    Indeed. But you wrote “NO spring at all” in your other question and you didn’t list any piece that could provide injection out of the box. Anyway, check out the struts2-persistenceplugin, it might be enough for your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not SO Meta question. I am using SO only as example. In
This continues from a previous question . I tried the suggested fix to check
And my confusion with JSF continues. This is a continuation of a question asked
I am using Struts/JSP for a webapp. I have a page A where I
OK, knowledgeable programmer-types, please be gentle... I'm having trouble getting a very simple, one-view
This is not exactly a straight-out question because I have just solved it, but
simple eclipse question: is there a way i can make the 'down' key move
First of all, I've found a similar question but it does not do the
This is a moot question as I'm not on this project any more, but
I got some question is using ASIHTTPRequest to tracking Upload/Download progress This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.