aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes I have to use EJB in my project. Where? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "I have to use EJB in my project. Where?" Watch "I have to use EJB in my project. Where?" New topic
Author

I have to use EJB in my project. Where?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 26, 2003
Posts: 30767
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

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


Groovy
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
I think its better to use JPA.


Dilshan Edirisuriya SCJP1.4, SCWCD1.4, SCBCD 5
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 02, 2001
Posts: 697
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.


SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 26, 2003
Posts: 30767
    
156

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

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Jeanne Boyarsky:

Yes. A stateless session bean is a good place.


Thanks a ton Jeanne.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I have to use EJB in my project. Where?