ejbとjpaコントローラーを使用したいのですが、netbeansではコントローラーが自動的に生成されます...セッションBeanステートレスであるクラス(UniversidadServiceEJB)からjpaコントローラーを呼び出そうとすると、プロジェクトをデバッグし、UserTransactionとEntityManagerFactoryが正常に作成されますただし、jpaコントローラー(UniversityJpaController)でメソッドutx.beginを呼び出すと、次の例外がスローされます。
java.lang.IllegalStateException:操作は許可されていません。
com.sun.enterprise.transaction.UserTransactionImpl.checkUserTransactionMethodAccess(UserTransactionImpl.java:146)at com.sun.enterprise.transaction.UserTransactionImpl.begin(UserTransactionImpl.java:162)at controller.UniversidadJpaController.create(UniversidadJpaController.java:47 )at services.UniversidadServiceEJB.create(UniversidadServiceEJB.java:40)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.inv 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。comb(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 org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:49 )at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)atjava.lang.reflect。 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.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)........。container.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)atsun。 Reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)........。container.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)atsun。 Reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)........。invoke(DelegatingMethodAccessorImpl.java:43)........。invoke(DelegatingMethodAccessorImpl.java:43)........。
SessionBeanクラスは次のとおりです。
@Stateless(name="UniversidadJpa")
@Remote(IGestionUniversidad.class)
public class UniversidadServiceEJB {
@Resource
private UserTransaction utx;
@PersistenceUnit(unitName="ApplicationEJBPU")
EntityManagerFactory emf;
public void create(Universidad universidad) throws Exception {
try {
UniversidadJpaController universidadController = new UniversidadJpaController(utx,emf);
universidadController.create(universidad);
} catch (RollbackFailureException ex) {
Logger.getLogger(UniversidadServiceEJB.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(UniversidadServiceEJB.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
そして、jpacontrollerクラスは次のとおりです。
public class UniversidadJpaController implements Serializable {
public UniversidadJpaController(UserTransaction utx, EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Universidad universidad) throws RollbackFailureException, Exception {
if (universidad.getEstudiantes() == null) {
universidad.setEstudiantes(new ArrayList<Estudiante>());
}
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
List<Estudiante> attachedEstudiantes = new ArrayList<Estudiante>();
for (Estudiante estudiantesEstudianteToAttach : universidad.getEstudiantes()) {
estudiantesEstudianteToAttach = em.getReference(estudiantesEstudianteToAttach.getClass(), estudiantesEstudianteToAttach.getId());
attachedEstudiantes.add(estudiantesEstudianteToAttach);
}
universidad.setEstudiantes(attachedEstudiantes);
em.persist(universidad);
for (Estudiante estudiantesEstudiante : universidad.getEstudiantes()) {
Universidad oldUniversidadOfEstudiantesEstudiante = estudiantesEstudiante.getUniversidad();
estudiantesEstudiante.setUniversidad(universidad);
estudiantesEstudiante = em.merge(estudiantesEstudiante);
if (oldUniversidadOfEstudiantesEstudiante != null) {
oldUniversidadOfEstudiantesEstudiante.getEstudiantes().remove(estudiantesEstudiante);
oldUniversidadOfEstudiantesEstudiante = em.merge(oldUniversidadOfEstudiantesEstudiante);
}
}
utx.commit();
} catch (Exception ex) {
// try {
// utx.rollback();
// } catch (Exception re) {
// throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
// }
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
}
そして、永続性ユニットは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="ApplicationEJBPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>sqlServer</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
何が問題なのですか?..ありがとうございました...