Solved

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

Posted on 2011-09-27
6
261 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
ID: 36813680
Yes it possible. You will need to pass the persistence unit around via a utility EJB.
0
 
LVL 6

Author Comment

by:__geof__
ID: 36813963
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
ID: 36813974
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 6

Author Comment

by:__geof__
ID: 36814010
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
ID: 36863619
You can do that. Use a common EntityManager and refernece the beans from the stateless session beans.
0
 
LVL 6

Author Comment

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

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
Crystal Reports Licensing Questions 4 56
jsp CRUD operations with and without prepared statement also hibernatge 1 27
Java array 21 77
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 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