D-Dᴙum
D-Dᴙum

Reputation: 7890

Persisting an Object Whose Base Class is Not @Entity

I have a case where I would like to persist objects of a class from a third-party library. More specifically I would like to persist objects of type EPStatementObjectModel.

As far as I am aware this class is not marked as @javax.persistence.Entity but does implement Serializable but as it stands I cannot directly persist this class. If I understand persisting objects correctly, the top-level class must be the 'entity' class. Does this mean that my only way to persist this class is to use XML descriptors, (which I would like to avoid)?

Upvotes: 0

Views: 179

Answers (1)

Maciej Dobrowolski
Maciej Dobrowolski

Reputation: 12122

You have two options:

  1. You can use xml mappings in orm.xml file as suggested by pL4Gu33 in a comment under your question. This approach has one drawback - it requires EPStatementObjectModel class to have some kind of unique identifier - and as I can see it does not have public one.
  2. You can also extend EPStatementObjectModel class (as it is not final) and wrap all getters and setter in your own setter/getter methods. In this case you can freely add some fields to class such as unique identifier. Using this approach enables you to make use of annotations. It will be easy to use entities retreived from database in places where EPStatementObjectModel object is needed (up-casting), unfortunately persisting existing EPStatementObjectModel objects will be more difficult as you will have to transform them in your subclass (down-casting) - therefore it would be nice to implement something like copy constructor.

Upvotes: 1

Related Questions