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

  • SEARCH
  • Home
  • 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 7367989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:36:28+00:00 2026-05-29T03:36:28+00:00

First, I have a stateless bean which do a simple retreive, looking like this.

  • 0

First, I have a stateless bean which do a simple retreive, looking like this.

@Stateless
@LocalBean
public A {
    @PersistenceContext
    private EntityManager em;

    public MyEntity retrieveMethod(){
        em.createQuery(...).getSingleResult();
    }
}

I have a statefull bean used to manage long conversation with a remote client, it look like this :

@Statefull
@LocalBean
@TransactionAttribute(NOT_SUPPORTED)
public class B implements BRemote {
    @PersistenceContext(type = EXTENDED)
    private EntityManager em;

    @EJB
    A a;

    public void start(){
        OtherEntity oe = new OtherEntity();
        oe.setRelationMyEntitie(this.a.retrieveMethod());

        em.persist(oe);
    }

    @TransactionAttribute(REQUIRED)
    public void end(){
        em.flush();
    }
}

The problem come on executing em.persist(oe). oe has a reference to an instance of MyEntity which was loaded by another EntityManager. So em don’t know it complaining about persisting detached Entity.

I would like to know what is there is a way to avoid this problem. If there is no direct solution, what is the best pattern to adopt ?


EDIT: I don’t want to use a transaction on start() because in real application, the statefull bean is used to realize a complex entity model which need to be persist at once. I try to setup the pattern called session-per-conversation in described here http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/transactions.html#transactions-basics-apptx. So if I understand you right, the solution is to “use a transaction in start() method of bean B”, but if I do this, at the end of the method, the content is flushed to database and it’s not what I want.

Other solution I can see is to get MyEntity in the B’s EntityManager, so do a merge, or a em.find() or to delegate retrieveMethod to some DAO style class, using the em in parameter, and in bean A, do a simple delegation to the DAO, in bean B, call directly the DAO.

Any idea on what is the best approach ?

  • 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-05-29T03:36:29+00:00Added an answer on May 29, 2026 at 3:36 am

    Here is the solution I used :

    import java.lang.reflect.Field;
    
    import javax.annotation.Resource;
    import javax.interceptor.AroundInvoke;
    import javax.interceptor.InvocationContext;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    public class SessionPerConversationInterceptor {
        private final static ThreadLocal<EntityManager> s_thEntityManager = new ThreadLocal<>();
    
        @AroundInvoke
    public Object manageEntityManager(InvocationContext ctx) throws java.lang.Exception {
        EntityManager em = s_thEntityManager.get();
        if (em == null) {
            MasterPersistenceContext traversableEntityManager = ctx.getTarget().getClass().getAnnotation(MasterPersistenceContext.class);
            if (traversableEntityManager != null) {
                for (Field field : ctx.getTarget().getClass().getDeclaredFields()) {
                    if (field.getAnnotation(PersistenceContext.class) != null) {
                        field.setAccessible(true);
                        em = (EntityManager) field.get(ctx.getTarget());
                        s_thEntityManager.set(em);
    
                        try {
                            Object oRet = ctx.proceed();
                            return oRet;
                        } finally {
                            s_thEntityManager.set(null);
                        }
                    }
                }
            }
        } else if (ctx.getTarget().getClass().getAnnotation(MasterPersistenceContext.class) == null) {
            for (Field field : ctx.getTarget().getClass().getDeclaredFields()) {
                if (field.getAnnotation(PersistenceContext.class) != null) {
                    field.setAccessible(true);
    
                    EntityManager oldEntityManager = (EntityManager) field.get(ctx.getTarget());
    
                    field.set(ctx.getTarget(), em);
    
                    try {
                        Object oRet = ctx.proceed();
                        return oRet;
                    } finally {
                        field.set(ctx.getTarget(), oldEntityManager);
                    }
                }
            }
        }
    
        return ctx.proceed();
    }
    }
    

    I hope it can help (and I hope this a not so ugly/dumb solution).

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

Sidebar

Related Questions

I have the following code: @Name(myService) @Scope(ScopeType.APPLICATION) @Stateless public class MyService { private Service
I have a stateless bean resposible for persisting entities to a database. This stateless
I have my first professional assignment of making a website in which a photographer's
I have an EJB3 stateless session bean that uses some other ejbs to do
I have an error which I can't understand in my webservice method. First, here
In order to bring up the menu for a DataGridViewComboBoxCell, I first have to
First I have a Listbox and set the DataSource to a MyObjectCollection MyObjectCollection implements
First I have a PHP file that gets data and file from a HTML
I have First/Last/Previous/Next buttons that change the selected child node of a TreeViewItem. Setting
I have first: select url from urls where site = '$value' order by url

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.