JPA EntityManager无法查询实体,恢复旧代码仍无效求助
Hey there, let's break down why your once-working EntityManager is now throwing errors in that Jersey endpoint. That truncated java.lang.Ille... error is a hint that something's off with the EM's state, access method, or underlying configuration—let's walk through the most likely fixes:
1. Stop manually fetching EntityManager from the context
Your current approach of grabbing the EM via context.getAttribute("em") is error-prone. The standard Java EE way to handle this (and far more reliable) is using dependency injection with @PersistenceContext:
// Inject the EntityManager directly instead of pulling from context @PersistenceContext(unitName = "YourPersistenceUnitName") private EntityManager em; @GET @Path("movie/{id}") @JSONP @Produces({"application/javascript", MediaType.APPLICATION_JSON}) public Movie getMovie(@PathParam("id") int id) { Movie requestedMovie = em.find(Movie.class, id); return requestedMovie; }
- Double-check that
YourPersistenceUnitNamematches exactly what's defined in yourpersistence.xmlfile. - This lets Glassfish handle the EM's lifecycle (opening, closing, managing transactions) automatically, avoiding issues with stale or closed EM instances.
2. Verify your EntityManager isn't in an invalid state
If you're still set on manual EM management, confirm the instance you're pulling from the context isn't already closed. Common scenarios:
- A previous part of your code calls
em.close()but doesn't refresh the context attribute with a new, open EM. - The EM was never properly initialized at app startup, so the context holds a null or uninitialized reference.
3. Check transaction and persistence configuration
- Transaction context: For read operations like
find(), Glassfish usually handles transactions automatically, but if your setup is misconfigured, adding@Transactionalto your method might resolve issues with inactive transaction contexts. - Persistence.xml validity: Ensure your persistence unit points to the correct MySQL JDBC datasource, with valid credentials and connection details. If the datasource itself is broken (e.g., MySQL is down, or Glassfish's JDBC pool is misconfigured), the EM can't function.
- Glassfish datasource check: Log into your Glassfish admin console, navigate to JDBC Connection Pools, and run a "Ping" test on your MySQL pool. If this fails, fix the pool connection first—it's almost certainly the root cause.
4. Rule out recent configuration changes
Did you update Glassfish/Jersey versions, modify web.xml, or tweak persistence settings lately? Even small changes (like switching Jersey entity providers) can break EM integration. Roll back recent changes temporarily to see if the issue resolves.
One last thing: Share the full error stack trace if you can—the truncated java.lang.Ille... only tells half the story. A full error (like IllegalStateException or IllegalArgumentException) will pinpoint exactly whether the EM is closed, uninitialized, or facing a configuration mismatch.
内容的提问来源于stack exchange,提问作者quantumbutterfly

