Using Standless EJB beans in Entity Bean

Obviously, using the attribute without preserving the state of the EJB beans in the bean entity smells, but please consider the following scenario and tell me if you know about a better solution:

  • I have an InvoiceTemplate Entity bean with a NextInvoiceDate field
  • Generating NextInvoiceDate is a complex procedure and must be performed outside of the InvoiceTemplate class
  • NextInvoiceDate should be updated every time InvoiceTemplate is stored in db

At the moment, I have logic regarding the generation of NextInvoiceDate in @PrePersist @PreUpdate methon in the InvoiceTemplate entity bean. The logic is becoming more complex, and I want to move it outside of the InvoiceTemplate bean. It seems to me that NextInvoiceDate must be a service to calculate NextInvoiceDate . But then, is it correct to call this service from within the InvoiceTemplate ?

+6
java jpa
source share
2 answers

It's not that kind of smell - it's rather domain-centric design.

I do not know how to do this automatically, but you can:

  • in the beans session where you are processing your Invoicetemplate , enter an auxiliary bean that has logic to calculate the next date
  • create a private field with an installer on the entity and before you start using it, call entity.setNextDateHelper(..)

You can also check that AspectJ does not offer some EJB parameters so that you can enter EJB whenever an object is of a given type ( Invoicetemplate ). AspectJ works with spring beans, I don't know if there are any such options for EJB.

+3
source share

Do you need something complicated, like a service or an EJB? Can you just write a static method (perhaps in a utility class) to keep the logic? Usually I am pretty biased towards such things, but if all you have is complex logic that does not require any interaction with the database or collaboration with objects, this may be the purest approach.

0
source share

All Articles