In my project , I use JSF+JPA+CDI+WildFly 8.2 in the persistence layer. I have a BasicDao , like this:
public class BasicDao<M, K> {
private org.jboss.logging.Logger logger = org.jboss.logging.Logger
.getLogger("BasicDao");
@Inject
@Primary
protected EntityManager em;
Class<M> mclass;
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
@Transactional(value=TxType.NOT_SUPPORTED)
public M find(K id){
return em.find(mclass, id);
}
@Transactional(value=TxType.REQUIRED)
public void insert(M inst){
this.em.persist(inst);
}
@SuppressWarnings("unchecked")
@Transactional(value=TxType.REQUIRED)
public K insertWithAutoId(M inst){
this.em.persist(inst);
return (K) this.em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(inst);
}
@Transactional(value=TxType.REQUIRED)
public M update(M updated){
return this.em.merge(updated);
}
@Transactional(value=TxType.REQUIRED)
public void delete(M nonUsed){
this.em.remove(nonUsed);
}
}
and the other DAOs like this :
@RequestScoped
public class UserDao extends BasicDao<User, Integer>{
@Transactional(value = TxType.NOT_SUPPORTED)
public User find(Integer id) {
return em.find(User.class, id);
}
@Transactional(value = TxType.REQUIRED)
public void insert(User inst) {
this.em.persist(inst);
}
@Transactional(value = TxType.REQUIRED)
public Integer insertWithAutoId(User inst) {
this.em.persist(inst);
return (Integer) this.em.getEntityManagerFactory()
.getPersistenceUnitUtil().getIdentifier(inst);
}
@Transactional(value = TxType.REQUIRED)
public User update(User updated) {
return this.em.merge(updated);
}
@Transactional(value = TxType.REQUIRED)
public void delete(User nonUsed) {
this.em.remove(nonUsed);
}
@SuppressWarnings("unchecked")
@Transactional(value = TxType.NOT_SUPPORTED)
public User findByName(String loginName) {
Query query = em.createQuery("select u from User u where u.loginName=:loginName");
List<User> users = (List<User>) query.setParameter("loginName",
loginName).getResultList();
return users.isEmpty() ? null : users.get(0);
}
}
In my ManageredBean,the code likes this:
@Inject
private UserDao userDao;
but it often throws exception, not always. I was very confused. The exception stack like this:
09:50:59,945 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-27) JSF1073:在 RENDER_RESPONSE 6 的处理过程中捕捉到 com.sun.faces.mgbean.ManagedBeanCreationException:UIComponent-ClientId=,Message=对受管 bean resumeAudit 执行资源注入 (resource injection) 时发生错误
09:50:59,946 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-27) 对受管 bean resumeAudit 执行资源注入 (resource injection) 时发生错误: com.sun.faces.mgbean.ManagedBeanCreationException: 对受管 bean resumeAudit 执行资源注入 (resource injection) 时发生错误
at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:227) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116) [javax.el-3.0.1-b05.jar:]
at com.sun.el.parser.AstValue.getBase(AstValue.java:151) [javax.el-3.0.1-b05.jar:]
at com.sun.el.parser.AstValue.getValue(AstValue.java:200) [javax.el-3.0.1-b05.jar:]
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226) [javax.el-3.0.1-b05.jar:]
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.2.8-jbossorg-1.jar:]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.UIOutput.getValue(UIOutput.java:174) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.java:120) [jsf-impl-2.2.8-jbossorg-1.jar:]
at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129) [jsf-impl-2.2.8-jbossorg-1.jar:]
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133) [jsf-impl-2.2.8-jbossorg-1.jar:]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.8-jbossorg-1.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) [jsf-impl-2.2.8-jbossorg-1.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) [jboss-jsf-api_2.2_spec-2.2.8.jar:2.2.8]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at com.wangyin.hzcrp.auth.AuthFilter.doFilter(AuthFilter.java:109) [classes:]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_75]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_75]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_75]
Caused by: com.sun.faces.spi.InjectionProviderException: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
at org.jboss.as.jsf.injection.JSFInjectionProvider.invokePostConstruct(JSFInjectionProvider.java:63) [wildfly-jsf-injection-8.2.0.Final.jar:8.2.0.Final]
at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221) [jsf-impl-2.2.8-jbossorg-1.jar:]
... 70 more
Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:162)
at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:133)
at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:100)
at org.jboss.as.ee.component.ComponentRegistry$ComponentManagedReferenceFactory.getReference(ComponentRegistry.java:171)
at org.jboss.as.ee.component.ComponentRegistry.createInstance(ComponentRegistry.java:87)
at org.jboss.as.web.common.WebInjectionContainer.newInstance(WebInjectionContainer.java:77)
at org.jboss.as.jsf.injection.JSFInjectionProvider.invokePostConstruct(JSFInjectionProvider.java:61) [wildfly-jsf-injection-8.2.0.Final.jar:8.2.0.Final]
... 71 more
Caused by: org.jboss.weld.exceptions.WeldException: WELD-000049: Unable to invoke public void com.wangyin.hzcrp.controller.ResumeAudit.init() on com.wangyin.hzcrp.controller.ResumeAudit@628e5ac9
at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.java:100) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.postConstruct(DefaultLifecycleCallbackInvoker.java:81) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.producer.BasicInjectionTarget.postConstruct(BasicInjectionTarget.java:126) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.producer.BeanInjectionTarget.postConstruct(BeanInjectionTarget.java:70) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.as.weld.deployment.processors.WeldComponentIntegrationProcessor$3.run(WeldComponentIntegrationProcessor.java:196) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.as.weld.deployment.processors.WeldComponentIntegrationProcessor$AbstractInjectionTargetDelegatingInterceptor.processInvocation(WeldComponentIntegrationProcessor.java:260) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:53) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.java:56) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.weld.injection.WeldInjectionContextInterceptor.processInvocation(WeldInjectionContextInterceptor.java:43) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45) [wildfly-ee-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326)
at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:160)
... 77 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_75]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_75]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_75]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_75]
at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.java:98) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
... 100 more
Caused by: java.lang.IllegalArgumentException: Can not set javax.persistence.EntityManager field com.wangyin.hzcrp.storage.dao.BasicDao.em to com.wangyin.hzcrp.storage.dao.ResumeDao$Proxy$_$$_WeldSubclass
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75) [rt.jar:1.7.0_75]
at java.lang.reflect.Field.set(Field.java:741) [rt.jar:1.7.0_75]
at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:94) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:70) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:121) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:150) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:98) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:78) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at com.wangyin.hzcrp.storage.dao.ResumeDao$Proxy$_$$_WeldClientProxy.find(Unknown Source) [crp-storage-1.0.0-SNAPSHOT.jar:]
at com.wangyin.hzcrp.controller.ResumeAudit.init(ResumeAudit.java:81) [classes:]
... 105 more
Can someone help me ? Thanks a million.
You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet). You can use @PersistenceContext without specifying a unitName attribute to use the OC4J default persistence unit, as Example 29-12 shows.
An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed.
Several entity manager factories can be prepared for connecting to different data stores. JPA EntityManager is used to access a database in a particular application. It is used to manage persistent entity instances, to find entities by their primary key identity, and to query over all entities.
The complete example of getting EntityManager using the custom configuration in Spring Boot. Open eclipse and create maven project, Don't forget to check 'Create a simple project (skip)'click on next. Fill all details(GroupId – entitymanager, ArtifactId – entitymanager and name – entitymanager) and click on finish.
The appropriate way to inject a Persistent Context into a managed Bean is the following:
@PersistenceContext(unitName = "my-pu-name")
private EntityManager em;
Note that this can only be done on managed beans, ie, beans that are created/destroyed by the container.
A best practice is to add this as as a producer, so that you can just use @Inject
into your managed beans:
@ApplicationScoped
public class ApplicationResources {
@Produces
@PersistenceContext(unitName = "my-pu-name")
private EntityManager em;
}
Then, you can inject it in JAX-RS endpoints, EJBs and any other bean that is managed by the container:
@Stateless
public class FooService {
@Inject
EntityManager em;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With