Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I have to use EJB in my project. Where?

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class which performs database operations. I think, I should make it an "EJB� class.

Now, what kind of bean it can be out of stateless and stateful session bean?

I don�t need any previous interaction information in next interaction (don�t require conversational communication). So, stateful is not a good idea, Right?

Stateless should do the job. Right?

Or something else I should use???

Note: I have to use EJB, somehow, somewhere.

Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34378
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur,
You are correct that a stateless session bean is the simplest one you could use. Another (more complex) alternative is to use JPA/entity beans for the data access itself.

Is this for a class? It's ok if it is. Just seems like an odd requirement to say you have to use EJB without more details.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: I have to use EJB, somehow, somewhere.

Why do have to use EJB somehow ?
Better to use JPA.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its better to use JPA.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradip Bhat:

Why do have to use EJB somehow ?
Better to use JPA.


Thanks Jeanne and Pradip.

I AM using JPA. But that's for data (entity classes). Where the code of operating on data (code which create EntityManagerFactory, EntityManager etc objects) should go? In stateless session bean???

Thanks.
 
Paul Michael
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Session Beans, you can directly inject EntityManager into your bean using PersitentContext annotation. (if I remember it correctly)

EntityManager is not thread safe, but since the EJB container guarantees that only 1 thread accesses a session bean at a time then it won't be a problem.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at this thread for more information: http://www.coderanch.com/t/217985/ORM/java/where-JPA-code-should-go.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34378
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ankur rathi:
Where the code of operating on data (code which create EntityManagerFactory, EntityManager etc objects) should go? In stateless session bean???

Yes. A stateless session bean is a good place.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:

Yes. A stateless session bean is a good place.


Thanks a ton Jeanne.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:

Yes. A stateless session bean is a good place.


Got a doubt. I think, I should write that code in DAO class, and 'creating DAO factory, DAO object etc' code should go in stateless session bean. What's say???

Thanks.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can certainly inject EntityManager into your bean class and not bother with a DAO. The counter argument is that the DAO is still needed as it provides a single interface to the client by encapsulating data access logic.

If you will go with the DAO option ... your bean method will obtain an instance of the DAO class (possibly via a factory) and invoke the DAO method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic