6

I am Thinking of creating A JPA 2.0 Mapping of an entity, however I do not want to have any setters or expose a constructor, I want to use a Factory to create my class whenever it is fetched from the db. I have taken a look at the pro JPA 2.0 book and some articles online, but cannot find anything similar, has anyone done anything like this?

Thank you,

GMelo
  • 229
  • 1
  • 16

2 Answers2

0

Use field access and add a private constructor. JPA can access private fields.

James
  • 17,965
  • 11
  • 91
  • 146
0

Factory pattern actually is a good candidate for JPA Entities. However it is practically impossible to make entity manager use your custom factory on fetching unless you make your own JPA implementation. But there are still many applications of factory, especially for CRUD programms. The most important IMHO is context depending defaults for fields, concerning user created new entities of course. For full stack java-ee applications I prefer to use Singleton EJB for this purpose, as it has easy access to JPA Entity manager. You can make protected constructor for entity to prevent user instantiation.

Sasa7812
  • 583
  • 1
  • 7
  • 11