简体   繁体   English

将对象保存到数据库时使用休眠

[英]Use hibernate when saving objects to the database

In my java application I have some serialized entity classes with inheritance. 在我的Java应用程序中,我有一些带有继承的序列化实体类。 When saving instances of these classes i am converting them to a byte array and saving to a longblob column in my database table. 保存这些类的实例时,我将它们转换为字节数组,并保存到数据库表中的longblob列中。 Is there any advantage using hibernate to implement this program. 使用休眠实现该程序有什么好处。 Because as far I understand hibernate is used to map entities with database tables in a proper way. 因为据我所知,休眠是用于以适当方式将实体与数据库表映射的。 But here I don't have a relational model to map attributes of entities. 但是这里我没有一个关系模型来映射实体的属性。 I am saving them as objects. 我将它们保存为对象。 Am I missing something. 我错过了什么吗? Please clarify me. 请澄清一下。 Thanks in advance. 提前致谢。

If you don't have a relational data model to save those objects and you can't change your schema, then you can use your current approach. 如果没有用于保存这些对象的关系数据模型,并且无法更改架构,则可以使用当前方法。

If you use PostgreSQL you might be interested in JSON storage as well. 如果使用PostgreSQL,您可能也会对JSON存储感兴趣。 That way you can store your hierarchies using JSON objects and you can even run native SQL queries against them (although not inheritance-aware, but you can cope with that if you use some _class column to differ between object types). 这样,您可以使用JSON对象存储层次结构,甚至可以对它们运行本机SQL查询(尽管不支持继承,但是如果使用一些_class列来区分对象类型,则可以解决此问题)。

The cleanest approach is to have the relation model in sync with your business domain model. 最干净的方法是使关系模型与您的业务域模型同步。 That way you can benefit from: 这样,您可以受益于:

  • optimistic locking ( preventing lost updates phenomena ) 乐观锁定( 防止丢失更新现象
  • caching (2nd level cache and query cache) 缓存(二级缓存和查询缓存)
  • query-able hierarchies 可查询的层次结构
  • an external DBA hierarchies could run an update on your hierarchies using mere SQL 外部DBA层次结构可以仅使用SQL在您的层次结构上运行更新
  • auditing 审计

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

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