Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using stateless EJB beans in an Entity Bean

Tags:

java

jpa

ejb-3.0

Obviously using stateless EJB beans in an entity bean smells, but please consider a scenario as follows and tell me if you know of a better solution:

  1. I have an InvoiceTemplate Entity Bean with field NextInvoiceDate
  2. Generating NextInvoiceDate is a complex procedure and should be performed outside of the InvoiceTemplate class
  3. NextInvoiceDate should be updated each time InvoiceTemplate is stored to the db

For now I have logic regarding the generation of NextInvoiceDate in @PrePersist @PreUpdate methon in InvoiceTemplate entity bean. The logic is getting more and more complicated and I want to move it outside of the InvoiceTemplate entity bean. It looks to me that there should be a service to calculate NextInvoiceDate. But then is it right to invoke this service from the inside of InvoiceTemplate?

like image 366
mgamer Avatar asked Sep 13 '10 11:09

mgamer


People also ask

What is stateless session bean in EJB What is entity bean in EJB?

A stateless session bean does not store session or client state information between invocations—the only state it might contain is not specific to a client, for instance, a cached database connection or a reference to another EJB.

What is the valid reason behind a stateful session bean instead of a stateless session bean?

Stateful session beans maintain the state associated with a client. Each stateful session bean serves exactly one client. Stateless session beans are intended to be simple and lightweight; that is, they are easy to develop with low runtime resource requirements on the server.

What is session bean differentiate between stateful session bean and stateless session bean?

The main difference between Stateless and Stateful Session Bean is that Stateless Session Bean is a business object without state (data) that describes the business logic while Stateful Session Bean is a business object with a state (data) that describes the business logic.

Which annotation is used for stateless EJB?

I know that adding @Stateful or @Stateless annotations to a class will make it an EJB bean.


1 Answers

It isn't such a smell - it is a lean towards domain-driven design.

I don't know of any way to do this automatically, but you can:

  • in the session beans where you handle your Invoicetemplate, inject the helper bean that has the logic to calculate the next date
  • create a private field with a setter on the entity, and before you start using it call entity.setNextDateHelper(..)

You can also check whether AspectJ doesn't offer some EJB options so that you can inject the EJB whenever an entity of a given type (InvoiceTemplate) is created. AspectJ works like that with spring beans, I don't know whether there are such options for EJB.

like image 175
Bozho Avatar answered Sep 21 '22 01:09

Bozho