简体   繁体   English

使用休眠从框架持久化对象

[英]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. 该对象具有一个ID字段,它是一个字符串。 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. 它还包含应映射为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. 为您不维护的结构创建Hibernate映射很繁琐。
  • 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. 如果您升级了正在使用的第3方库的版本,则您的Hibernate映射可能不再匹配。
  • 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! 如果您已经考虑了所有这些,并且仍然需要保留该结构,请告诉我! :) :)

UPDATE 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. UPDATE我并不是要创建一个实现与第3方对象相同的接口的结构,而是要创建一个完全重做的结构,以适合关系数据库中的持久性。 Then the adapter will create the 3rd party structure based on your objects.. 然后,适配器将基于您的对象创建第三方结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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