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 6642527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:56:28+00:00 2026-05-25T23:56:28+00:00

I have Stateless Session Bean with Container-Managed Transactions. I want to return unmanaged entity

  • 0

I have Stateless Session Bean with Container-Managed Transactions. I want to return unmanaged entity after create (persist) it in a database. That’s how I do it:

@Stateless
public class MyBean {

   @EJB(name="MyController")
   private MyController myController;

   public MyEntity create(MyEntity entity) {
      //...
      myController.create(entity);
      myController.preTransfer(entity);
      return entity;
   }
}
@Stateless
public class MyController {

   @PersistenceContext(unitName = "myPU")
   private EntityManager em;

   public void create(MyEntity entity) {
      //...
      em.persist(entity);
   }

   public void preTransfer(MyEntity entity) {
       if (em.contains(entity)) {
           em.detach(entity);
       }
       //...
   }
}

I call MyBean.create, entity successfully persisted and MyBean.create return unmanaged entity, that’s ok. But next time when I try retrieve this entity by id, it can’t be found. If I comment detach, entity can be found, but MyBean.create return managed entity in that case. Where I am wrong ?

  • 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-25T23:56:28+00:00Added an answer on May 25, 2026 at 11:56 pm

    The javadoc of EntityManager.detach states:

    Unflushed changes made to the entity if any (including removal of the
    entity), will not be synchronized to the database

    So you’re persisting it, then detaching it. But the operations associated to persist have not been flushed yet, and the entity is thus not saved to the database.

    Why do you want to detach it? As soon as the transaction is ended, it will be detached automatically.

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

Sidebar

Related Questions

I have gone through various books on stateful and stateless session bean and how
I have just found RowSets for database querying with JDBC. They are stateless and
We have a strange situation where we lose a Stateless SessionBean in a Bean
What would a stateless session bean provide over just a regular class that has
We have some JavaEE5 stateless EJB bean that passes the injected EntityManager to its
Have a n-tire web application and search often times out after 30 secs. How
Have you managed to get Aptana Studio debugging to work? I tried following this,
Seam advises using an Extended persistent context in a Stateful Session Bean, in order
I am having some trouble accessing a stateful session bean (SFSB) from an application
I have been researching the implications of setting up a distributed session environment, and

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.