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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:24:45+00:00 2026-06-10T10:24:45+00:00

I would like to equip my EJBs with CRUD methods. I have many entities

  • 0

I would like to equip my EJBs with CRUD methods.

I have many entities and multiple persistence units.

I want to implement my CRUD methods once and invoke them on different persistence units.

I tried to achieve this using inheritance, but it isn’t working.

The CRUD class is:

public class FormEBean<T> {

    protected EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public String create(T entity) {
        try {
            em.persist(entity);
            em.flush();
            return null;
        } catch (Exception ex) {
            return ex.getLocalizedMessage();
        }
    }

    public void create(List<T> entityList) {
        for (T entity : entityList) {
            em.persist(entity);
        }
    }

    public void edit(T entity) {
        em.merge(entity);
    }

    public void edit(Set<T> entitySet) {
        Iterator<T> it = entitySet.iterator();
        while (it.hasNext()) {
            T entity = it.next();
            em.merge(entity);
        }
    }

    public void remove(T entity) {
        em.remove(em.merge(entity));
    }

    public void remove(T[] listaDaRimuovere) {
        for (T entity : listaDaRimuovere) {
            em.remove(em.merge(entity));
        }
    }

    public void remove(List<T> listaDaRimuovere) {
        for (T entity : listaDaRimuovere) {
            em.remove(em.merge(entity));
        }
    }
}

So I tryed in this way:

@Stateless
@Remote(MyEBeanRemote.class)
public class MyEBean extends FormEBean<MyEntityClass> implements MyEBeanRemote {

    @PersistenceContext(unitName = "siat-ejbPU")
    private EntityManager em;

    // code here
}

Even if I haven’t got any error, the CRUD functions have no effect on my DataBase.

Instead if I insert them directly into MyEBean it behaves as expected.

I don’t want to use @PersistenceContext(unitName = "siat-ejbPU") in the FormEBean, because EJBs could use different persistence units.

Is there a way to solve this problem?

Is there a pattern I can use to reuse my code?

Edit:

The aim of this question is finding a solution that maximizes CRUD code reuse in EJBs belonging to different EJB modules and having different persistence units.

Using generic methods in a stateless session bean seems like a good solution, but only for reusing CRUD code for EJBs in the same persistence unit.

What solution could be persistence unit independent (if it exists)?

  • 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-10T10:24:46+00:00Added an answer on June 10, 2026 at 10:24 am

    I found a solution that solves the problem.

    It’s based on jahroy’s answer, but uses inheritance to deal with multiple persitence units.

    The common code is a base class (not generic but with generic methods):

        public class FormEBean {
    
            final Logger logger = LoggerFactory.getLogger(FormEBean.class);
    
            protected EntityManager emCrud;
    
            public EntityManager getEmCrud() {
                return emCrud;
            }
    
            public void setEmCrud(EntityManager em) {
                emCrud = em;
            }
    
            public <T> String create(T entity) {
                String exception = null;
                try {
                    emCrud.persist(entity);
                    emCrud.flush();
                } catch (Exception ex) {
                    //ex.printStackTrace();
                    exception = ex.getLocalizedMessage();
                }
                return exception;
            }
    
            public <T> void create(List<T> entityList) {
                for (T entity : entityList) {
                    emCrud.persist(entity);
                }
            }
    
            public <T> void edit(T entity) {
                emCrud.merge(entity);
            }
    
            public <T> void edit(Set<T> entitySet) {
                Iterator<T> it = entitySet.iterator();
                while (it.hasNext()) {
                    T entity = it.next();
                    emCrud.merge(entity);
                    emCrud.flush();
                }
            }
    
            public <T> void remove(T entity) {
                emCrud.remove(emCrud.merge(entity));
            }
    
            public <T> void remove(T[] listaDaRimuovere) {
                for (T entity : listaDaRimuovere) {
                    emCrud.remove(emCrud.merge(entity));
                }
            }
    
            public <T> void remove(List<T> listaDaRimuovere) {
                for (T entity : listaDaRimuovere) {
                    emCrud.remove(emCrud.merge(entity));
                }
            }
         }
    

    … and this is the interface:

    public interface FormEBeanRemote {
        public void setEmCrud(EntityManager em);
        public <T> String create(T entity);
        public <T> void create(List<T> entityList);
        public <T> void edit(T entity);
        public <T> void edit(Set<T> entitySet);
        public <T> void remove(T entity);
        public <T> void remove(T[] listaDaRimuovere);
        public <T> void remove(List<T> listaDaRimuovere);
    }
    

    The EJB (stateless session bean) looks like this:

    @Stateless
    @Remote(MyEBean.class)
    public class MyEBean extends FormEBean implements MyEBeanRemote {
    
        final Logger logger = LoggerFactory.getLogger(MyEBean.class);
    
        @PersistenceContext(unitName = "siat-ejbPU")
        private EntityManager em;
    
        public EntityManager getEm() {
            return em;
        }
    
        public void setEm(EntityManager em) {
            this.em = em;
        }
    
        @PostConstruct
        public void postConstruct() {
            this.setEmCrud(em);
        }
    

    …where

    @Remote
    public interface MyEBeanRemote extends FormEBeanRemote {
    ......
    }
    

    Note that the EJB uses the postConstruct method to set the entityManager, which is delegated to perform CRUD operations on a specific persistence unit.

    It’s working like a charm so far.

    If someone finds any pitfalls please let me know.

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

Sidebar

Related Questions

As shown below I have created a List of equip.I would like to have
Would like to horizontally align multiple images and have them flow down the page
would like to put fieldSets side-by-side on my Edit page because I have so
Would like to know what does Drupal 7 and Acquia have in common? I
Would like to implement some solution for adding tags to a certain user profile
would like to know how can this be implemented in Joomla. I have a
would like to have a central place to register new signals, connect to signals,
Would like some advice from this. I got a table where I want to
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden

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.