servlets - EntityManagerFactory is not been injected - JPA -
i'm begginer jpa, , i'm having problems when utilize @persistenceunit in servlet. entitymanagerfactory not beingness injected, , don't know why.
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 { author w = response.getwriter(); w.append("hello hello"); w.close(); } }
i've configured persistence.xml file (inside meta-inf directory) this:
<?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>
why not entitymanagerfactory beingness injected?
i'm working jboss 4.2.3, , have configured datasource oracle-ds.xml file.
update:
if seek entitymanagerfactory programatically, works well:
entitymanagerfactory emf = persistence.createentitymanagerfactory("middlewareexpedelectrmodel");
but entitymanagerfactory instance via injection. thanks!
can seek
public class myservlet extends httpservlet { //this thread-safe @persistenceunit(unitname="middlewareexpedelectrmodel") private entitymanagerfactory emf;
as far know, methods in entitymanager interface not thread-safe, , may not shared among multiple concurrent requests. therefore, not inject entitymanager servlet instance variable.
this not thread-safe, , avoid it
@persistencecontext(unitname="middlewareexpedelectrmodel") private entitymanager em;
you can still inject entitymanager @ servlet class type level, , when needed during request processing.
@persistencecontext(unitname="middlewareexpedelectrmodel", name="persistence/em") public class myservlet extends httpservlet { protected void doget( httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { seek { initialcontext ic = new initialcontext(); entitymanager em = (entitymanager) ic.lookup("java:comp/env/persistence/em"); } grab (namingexception ex) { ... }
servlets jpa
No comments:
Post a Comment