Regarding to my opinion, there are 2 types of entity attributes (fields). The one must be persisted at the storage, the data base is an example, the another one is the temporary which is available for each business, the calculated attributes(fields), the age which is calculated from the date of birth for example.
Since it is a quick travel for updating and capturing the new technology, I do not have enough time to move further to the JPA Entity inheritance. I decide to create a wrapping class which contains the JPA Entity as its attribute and adding the other attributes which is a temporary entity attributes (fields). I will use it as a data bean for integrating among my application from end-to-end, the presentation layer through the JPA layer.
I hope I may have more time to come back to this state and investigating further about the good practice for the extended JPA entity.
As I’ve mentioned at my previous post Apache Derby Part 3, about the JPA preparation as creating 3 tables, the master table, the detail table which is one-to-many relationship with master and the look up table which is one-to-one relationship to the master.
I’ve created these 3 tables in Apache Derby by mentioning the relationship explicitly via the foreign key constraint. Then I use NetBeans to generate the JPA and create some testing about the basic CRUD and take more attention to the child data management via the POJO accessing through LIST or COLLECTION e.g. create new elements, update or delete elements. For sure, when save the parent object, all related child objects changing will be applied automatically without any line of code.
I finally found it is named a cascading event and orphan removal based on the relationship among the entities. Then it’s quite lucky I define that relationship explicitly so that the further investigating and trying is not required. I, personally, though It is nice to follow the standard.
Please note, my JPA example code follows the “Java EE 5 Tutorial”, Part V: Persistence”. You can download it from Sun/Oracle web site or search it via Google, it will take you to the downloading area.
Even I follow the Java EE 5 example code, it is worked fine at the Java EE 6: JPA 2.0 as well. The difference is via the JPA 1.x, I need to manage the detail changing by implement some coding and keep tracking and maintaining states by myself explicitly, but not for the JPA 2.0, all changing will be done by the JPA engine as mentioned above.
As the subject of this article, it is a basic starting. I will quick jump to the EJB 3.1 with purpose to capture and understand the whole big picture of new technology as fast as possible. I prefer the iterative action for my experimental learning by starting from the total picture and the move further to the detail part over and over.