Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Is it possible to distribute/share a persistence unit to multiple EJB modules containing entities?

Hi Experts.

In a J2EE 6 EAR, I would like to know if there is any means to distribute/reuse a persistence unit through multiple EJB modules.

As I understand, the unit seems limited to a single module. But I just want to check with you.
And if it is the case, does anyone have recommandation about how it should be implemented? Should entities always be packed in a single module?

Thanks for any lead.
0
__geof__
Asked:
__geof__
  • 3
  • 3
2 Solutions
 
a_bCommented:
Yes it possible. You will need to pass the persistence unit around via a utility EJB.
0
 
__geof__Author Commented:
Thanks a_b.

If I understand you correctly and take a starting point in an EAR, I must add my utility EJB in the lib?

Is it also possible that Netbeans doesn't recognize this setup?
0
 
a_bCommented:
Not necessarily in the lib. What you could do is create one EJB in your ear that has the persistence unit and then pass that ejb to other ejbs to use the persistence unit.
Eg.
@Stateles....
public class EJBUtils
{
      @PersistenceUnit(...)
      EntityManager em;

       Create getters and setters
}

@Stateless
public class EmployeeBean
{

    @EJB
    EJBUtilsLocal ejbUtils;

    //Now via ejbUtils you can get a a hold of the entity manager you created.

}

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
__geof__Author Commented:
Just to be precise, I'm talking about Entity Beans (with annotation mapping to the database).
0
 
a_bCommented:
You can do that. Use a common EntityManager and refernece the beans from the stateless session beans.
0
 
__geof__Author Commented:
OK . Thanks. I'll check it out and come back to you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now