我是JPA的佼佼者,当我在servlet中使用@PersistenceUnit时,我遇到了问题。 EntityManagerFactory没有被注入,我不知道为什么。
public class MyServlet extends HttpServlet
{
@PersistenceUnit
private EntityManagerFactory emf = null;
public MyServlet ()
{
super();
}
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Writer w = response.getWriter();
w.append("HELLO HELLO");
w.close();
}
}
我已经配置了persistence.xml文件(在META-INF目录中),如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MiddlewareExpedElectrModel" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/OracleDS</non-jta-data-source>
<class>com.ieci.mugeju.middleware.model.entities.FechaUltimaSolicitudProcesada</class>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
</properties>
</persistence-unit>
为什么不注入EntityManagerFactory?
我正在使用Jboss 4.2.3,我还使用oracle-ds.xml文件配置了数据源。
更新
如果我尝试以编程方式获取EntityManagerFactory,则效果很好:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MiddlewareExpedElectrModel");
但是我希望通过注入获得一个EntityManagerFactory实例。 谢谢!
答案 0 :(得分:0)
你能试试吗
public class MyServlet extends HttpServlet {
//this is thread-safe
@PersistenceUnit(unitName="MiddlewareExpedElectrModel")
private EntityManagerFactory emf;
据我所知,EntityManager接口中的方法不是线程安全的,并且可能不会在多个并发请求之间共享。因此,不要将EntityManager注入servlet实例变量。
这不是线程安全的,并且避免使用
@PersistenceContext(unitName="MiddlewareExpedElectrModel")
private EntityManager em;
您仍然可以在servlet类类型级别注入EntityManager,并在请求处理期间需要时查找它。
@PersistenceContext(unitName="MiddlewareExpedElectrModel", name="persistence/em")
public class MyServlet extends HttpServlet {
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
InitialContext ic = new InitialContext();
EntityManager em =
(EntityManager) ic.lookup("java:comp/env/persistence/em");
} catch (NamingException ex) {
...
}
答案 1 :(得分:0)
我在glassfish服务器上的应用程序中遇到了同样的问题。 直到我从web.xml中删除servlet声明并且使用批注声明servlet时,entitymanager工厂始终为空。
@WebServlet("/GetRevisioniServlet") // <-- this annotation solved all my problems
public class GetRevisioniServlet extends HttpServlet {
@PersistenceUnit(unitName = "clerpPU")
private EntityManagerFactory entityManagerFactory;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EntityManager em = null;
em = entityManagerFactory.createEntityManager();
.....
}
}