2

我正在考虑创建实体的 JPA 2.0 映射,但是我不想有任何设置器或公开构造函数,我想在从数据库中获取类时使用工厂来创建我的类。我在网上看了一下pro JPA 2.0的书和一些文章,但找不到类似的东西,有没有人做过这样的事情?

谢谢,

4

2 回答 2

1

使用字段访问并添加私有构造函数。JPA 可以访问私有字段。

于 2013-09-18T12:43:09.837 回答
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.

于 2013-10-03T06:28:53.873 回答