Solved

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

Posted on 2011-09-27
6
249 Views
Last Modified: 2012-05-12
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
Comment
Question by:__geof__
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:a_b
Comment Utility
Yes it possible. You will need to pass the persistence unit around via a utility EJB.
0
 
LVL 6

Author Comment

by:__geof__
Comment Utility
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
 
LVL 20

Accepted Solution

by:
a_b earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Author Comment

by:__geof__
Comment Utility
Just to be precise, I'm talking about Entity Beans (with annotation mapping to the database).
0
 
LVL 20

Assisted Solution

by:a_b
a_b earned 500 total points
Comment Utility
You can do that. Use a common EntityManager and refernece the beans from the stateless session beans.
0
 
LVL 6

Author Comment

by:__geof__
Comment Utility
OK . Thanks. I'll check it out and come back to you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
@SBGen Method 3 25
bitbucket vs gitbucket 3 29
mockito example issue 8 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now