Search code examples
javajpaobject-persistence

A non pesistant super class for 5 persistant classes w/same 3 sometimes persistant fields?


I can't solve this problem.
Can anyone tell me it it is possible to solve it?

I have a five-table database that requires 5 persistent classes.

All 5 need to carry these 3 String fields:
field1, field2, field3.

HOWEVER. Each of the 5 persistent classes makes different different combinations of them persistent with the others @transient. Here are the five persistent classes:

    Persistent         @Trasient
1   field1, field2     field3
2.  all 3              none
3.  field2             field1, field3
3.  none               all 3
4.  field1, field3     field 2
5.  field 1            field2, field3

I'd love to super class the get/set methods.

(There is a lot of code used to test them before accepting them that is identical.)

Is it possible?

I think the answer is NO. But I thought I'd check.

Thanks.


Solution

  • This seems to be one of the use-cases that are not suitable for annotation metadata.

    JPA allows you to override, supplement or replace annotations with XML metadata. In a scenario like you described, I think it would be best to define the persistent/transient fields in XML metadata.

    You may want to check the hibernate docs or this closed StackOverflow question for more details.