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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:21:20+00:00 2026-06-11T19:21:20+00:00

I have a Java EE 6 web application using container-managed transactions and container-propagated Persistence

  • 0

I have a Java EE 6 web application using container-managed transactions and container-propagated Persistence Context through

@PersistenceContext
private EntityManager em;

In the JPA layer I have an inheritance strategy, where MyExtendedClassA and MyEntendedClassB both extend abstract MyClass.

I use stateless facade service classes for implementing the data access through the methods find, findAll, merge, persist, remove:

@Stateless
public class MyExtendedClassAFacade {
    @PersistenceContext
    private EntityManager em;
    public void persist(MyExtendedClassA a) {
        // ...
    }
    // other methods
}

So far, so good. Now I have to implement a polymorphism on the behavior of the extended classes. This behavior consists in managing some other entities in the database, so needs the PersistenceContext (and hence I need to use other Stateless EJBs):

@Stateful
public class MyBean {

    @EJB
    private MyClassFacade myClassFacade;   // stateless facade service class

    public void doSomething() {
        for (MyClass c : myClassFacade.findAll()) {
            // here I need to perform operations on the db. 
            // The implementation differs on each extended class.
            // I want to avoid a brute-force coding like:
            if (c.getClass().equals(MyExtendedClassA.class)) {
                @EJB MyExtendedClassAFacade myClassFacadeA;
                myClassFacadeA.doSomething((MyExtendedClassA) c);
            } else if (c.getClass().equals(MyExtendedClassB.class))
                @EJB MyExtendedClassBFacade myClassFacadeB;
                myClassFacadeB.doSomething((MyExtendedClassB) c);
            }
            // Instead, I would like to write something like:
            @EJB AnotherStatelessBean asb;
            asb.doSomething(c);
        }
    }
}

Is there any abstraction pattern I can use for this goal?

  • 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-11T19:21:22+00:00Added an answer on June 11, 2026 at 7:21 pm

    This is an EJB Polymorphism and can be applied to other situations. It doesn’t relate to JPA inheritance directly.

    The trick is to use @Local interface for EJBs:

    @Local
    public interface MyClassFacadeInterface {    
        public void doSomething(MyClass c);
    }
    

    and let the existing Facade Stateless beans for the extended classes implement that interface.

    After that, the Facade stateless implementation classes must be looked for using InitialContext.lookup("java:module/MyExtendedClassAFacade"). Here, the trick is to give the facade classes names that are related to the entity classes in order to facilitate the lookup. The code to be used in the business layer is:

    public void doSomething() {      
        for (MyClass c : myClassFacade.findAll()) {      
            String lookupName = getNameFromClassName(c.getClass().name());
            MyClassFacadeInterface myInt =  (MyClassFacadeInterface) new InitialContext().lookup(lookupName);
            myInt.doSomething(c);
        }      
    }      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My web application is written using jsp/javascripts. Backend Java. Have managed to implement the
I have a Java based web-application using Java Server Faces and Facelets. I am
I have a java web application which I can debug using IntelliJ or Eclipse
I have built a web application using Java EE platform that sells one of
I have web application written in java using Eclipse. It has just one servlet
I'm using Java with JSF an Glassfish 3 container. In my web application I'm
I have web services written using java which interacts with the android application only
I have a Java EE web application using features from the Java EE 6
I'm building a personal Web application using Java EE 6 Technologies (the container is
I have a Java web application at my work and I'd like simplify how

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.