2013-04-25 17 views
4

我是JPA的新手。我正在嘗試創建EJB 3.0 + JPA(休眠)應用程序。當我將數據存入數據庫時​​,我得到了異常。獲取異常 - java.lang.IllegalStateException:一個JTA EntityManager不能使用getTransaction()

SessoinBean方法

@PersistenceUnit private EntityManagerFactory emf; 

@Override 
@TransactionAttribute(TransactionAttributeType.REQUIRED) 
public void insterCustomerDetails(Customer customer) { 
    // TODO Auto-generated method stub 
    try{ 
     //System.out.println("properties:::"+em.getProperties()); 
     //System.out.println("Model :::"+em.getMetamodel()); 
    System.out.println("Name ::::" + customer.getName() + "::customer id ::"+customer.getCustomer_id()+"::email::"+customer.getEmail_id()+"::address::"+customer.getAddress()+ 
      ":::ph number::"+customer.getPhNumber()); 
    EntityManager em = emf.createEntityManager(); 
    EntityTransaction et = em.getTransaction(); 
    et.begin(); 
    em.persist(customer); 
    et.commit(); 
    em.close(); 
    }catch(Exception e){ 
    e.printStackTrace(); 
} 
} 


的persistence.xml

<persistence-unit name="RetailUnit" > 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <!-- the JNDI data source--> 
    <jta-data-source>java/customer</jta-data-source> 
    <properties> 
     <!-- if this is true, hibernate will print (to stdout) the SQL it executes, 
      so you can check it to ensure it's not doing anything crazy 
      <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/> 
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/company;create=true" /> 
      <property name="hibernate.connection.username" value="user" /> 
      <property name="hibernate.connection.password" value="123" /> --> 
     <property name="hibernate.show_sql" value="true" /> 
     <property name="hibernate.format_sql" value="true" /> 
     <!-- since most database servers have slightly different versions of the 
      SQL, Hibernate needs you to choose a dialect so it knows the subtleties of 
      talking to that server --> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> 
     <property name="hibernate.archive.autodetection" value="class"/> 
     <!-- this tell Hibernate to update the DDL when it starts, very useful 
      for development, dangerous in production --> 
     <property name="hibernate.hbm2ddl.auto" value="create" /> 
    </properties> 
</persistence-unit> 


異常

SEVERE: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction() 
    at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:985) 
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.getTransaction(EntityManagerWrapper.java:857) 
    at retail.ejb.service.CustomerSessionBeanImpl.insterCustomerDetails(CustomerSessionBeanImpl.java:24) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) 
    at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:206) 
    at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79) 
    at $Proxy248.insterCustomerDetails(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:241) 
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:152) 
    at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:227) 
    at retail.ejb.service.__CustomerSessionBeanRemote_Remote_DynamicStub.insterCustomerDetails(retail/ejb/service/__CustomerSessionBeanRemote_Remote_DynamicStub.java) 
    at retail.ejb.service._CustomerSessionBeanRemote_Wrapper.insterCustomerDetails(retail/ejb/service/_CustomerSessionBeanRemote_Wrapper.java) 
    at retail.web.mbean.CustomerMB.createCustomer(CustomerMB.java:58) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:254) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
    at java.lang.Thread.run(Thread.java:722) 

請幫我在這。

回答

13

如果在EJB內部使用,則不需要手動使用事務 - 所有方法在默認情況下都是事務性的 - 您的方法甚至有@TransactionAttribute(TransactionAttributeType.REQUIRED),這是默認設置。

事務從方法啓動開始,如果沒有事務可用並且在方法返回時提交。無需手動交易控制。

如果你沒有一個令人信服的理由來使用EntityManagerFactory,它可能是更好注入EntityManager正確的,而不是從EMF檢索它在每次調用的:根據

@PersistenceContext 
private EntityManager em; 
+0

我做的代碼更改您的建議,但再次得到相同的異常。我已經提供了更新的代碼,請檢查。 – developer 2013-04-25 07:39:14

+1

Iam對不起。我沒有部署右耳。我再次部署,現在工作。非常感謝。 – developer 2013-04-25 07:45:18

+0

@developer - 讓我重申一下 - 新代碼會導致一個異常,並在'em.persist'上出現'JTA EntityManager不能使用getTransaction()'的消息?這會很奇怪,因爲代碼看起來不錯,而且實際上並沒有調用'getTransaction'。我認爲EJB是正確註釋的? – kostja 2013-04-25 07:45:53

相關問題