Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:10:44+00:00 2026-06-09T21:10:44+00:00

i want to use ejb and jpa controller, in netbeans the controller is generated

  • 0

i want to use ejb and jpa controller, in netbeans the controller is generated automatically… I try to call the jpa controller from class (UniversidadServiceEJB) that is a session bean stateless, I debugged the project and the UserTransaction and EntityManagerFactory is created successfully but when call the method utx.begin in the jpa controller (UniversityJpaController) throws this exception:

java.lang.IllegalStateException: Operation not allowed.

at 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.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 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)
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.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)
…
…
…

The Session Bean class is:

@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);
        } 

    } 

}

And the jpacontroller class is:

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();
            }
        }
    }
}

And the Persistence Unit is:

<?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>

Please what is the problem?..
Thank you very much…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:10:45+00:00Added an answer on June 9, 2026 at 9:10 pm

    Usually in an EJB Environment, the transaction is managed by the the container. It wraps the Bean methods in transactions with automatic rollback when an exception occurs. This also means that manually starting/committing/rollback a Transaction is not allowed and throws an IllegalStateException.

    Reference: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use two external jars from EJB: jar #1 - all interfaces
I want to use an EJB3 from within my Tapestry Page class, I can
i want use some data from a website with web service. i have a
Let's say I want to use an EJB container and I don't know how
From the CDI tutorial, i've discovered various examples and want to use them to
Is it possible to use EJB 3 with JDBC. I read somewhere, that it's
I want to define injection so that only if the injected interface has EJB
I have a @RequestScoped CDI bean that I want to turn into an EJB
I want use this 1 for using Bar code or QR code scanner. I
I want use BYTE_ORDER macro in my Xcode project but i can't because i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.