简体   繁体   中英

Persist Object from a framework using hibernate

I'm considering to persist a certain object from a library/toolkit I use. There are following issues:

The object has an id field which is a string. How can i store generated numeric value in it?

The object hierarchy of the library is rather complex and the according object is at the bottom. Also it contains arrays which should be mapped as OneToMany.

How can I create a mapping file for this object? Go through all parent classes to determine required fields? (source code is available). Or is there an option to persist all of them?

I would say, first of all, spend a bit of time analyzing your domain model.

Do you need to execute queries on these objects you want to save (besides read and save)? Will these queries be influenced by the dependencies these objects have? Are you really interested to persist all the graph of objects that your object is related to?

My first impression is that deciding to persist one object that comes out of a 3rd party library/framework is not a good idea because:

  • you don't have control on the object dependencies/relationships and on the object's lifecycle. It is created by that 3rd party, right?
  • you have to obey that particular structure, which most likely is not optimized/designed for persistance purposes. Creating Hibernate mappings for a structure not maintained by you is tedious.
  • you will end up persisting more information then you need
  • if you ever upgrade the version of the 3rd party library you are using, your Hibernate mappings may not match anymore.
  • there are a number of design principles which would be

I think you would be happier with designing your own structure and persist that. Then have an Adapter that converts your structure to the objects expected by the framework. This way, your code will be isolated by all the things the framework does or ever will do with these objects. Your persistance code remains the same, it's only the Adapter that might change.

I hope it makes sense. If you've already considered all this, and still need to persist that structure, let me know! :)

I am not referring to create a structure that implements the same interfaces as the 3rd party object, I was referring to create a completely re-worked structure, fitted for persistance in a relational database. 我并不是要创建一个实现与第3方对象相同的接口的结构,而是要创建一个完全重做的结构,以适合关系数据库中的持久性。 Then the adapter will create the 3rd party structure based on your objects..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM