简体   繁体   English

没有数据库映射的对象持久性

[英]Object persistence without database mapping

I am looking for an advice on persisting Java objects without the need for database mapping. 我正在寻找关于持久化Java对象而不需要数据库映射的建议。 In simple (and silly) words I want to have JPA, but not be bothered by details of database structure: tables, columns, keys. 简而言之,我想拥有JPA,但又不想被数据库结构的细节所困扰:表,列,键。 Ideally what I would like to achieve is just defining Java classes, instantiating objects and persisting/retrieving these objects and their associations without any need for repeating entities/properties information anywhere else. 理想情况下,我想要实现的只是定义Java类,实例化对象并持久化/检索这些对象及其关联,而无需在其他任何地方重复实体/属性信息。 I know that there are various kinds of NOSQL databases, document databases, key-value storages and so on, but I have close-to-zero experience with anything other than flavors of RDBMSes and by simply reading descriptions and manuals of Cassandra, BerkeleyDB, MongoDB etc I am even unable to judge whether they support my use cases or not ;( So here are my requirements: 我知道有各种各样的NOSQL数据库,文档数据库,键值存储等等,但是我对RDBMS风格以外的任何事物都有接近于零的经验,只需阅读Cassandra,BerkeleyDB的描述和手册, MongoDB等我什至无法判断它们是否支持我的用例;(所以这是我的要求:

  • I am going to use Java programming language, so it would be nice if there were some JARs with API to access database 我将使用Java编程语言,因此,如果有一些具有API的JAR来访问数据库,那就太好了
  • My main goal is to avoid the need of defining an object-to-table mapping, as I see it superfluous and I consider the type definition as the only necessary source of metadata for persistence. 我的主要目标是避免定义对象到表的映射,因为我认为它是多余的,并且我将类型定义视为持久性元数据的唯一必要来源。 Anything more is too much, maybe except some properties for fine-tuning (converters, transient fields, etc.) So if there is some JPA/RDBMS based solution that would let me completely avoid touching database schema, I take it. 什么都太多了,也许除了一些用于微调的属性(转换器,瞬态字段等)之外,因此,如果有一些基于JPA / RDBMS的解​​决方案可以让我完全避免接触数据库模式,那我就接受了。
  • Object graphs in my application are going to have many relationships, there is going to be no logical top-level object which aggregates other sub-objects as fully contained properties. 我的应用程序中的对象图将具有许多关系,不会有逻辑顶级对象将其他子对象聚合为完全包含的属性。 On the contrary, my model contains quite a few top level classes, which will be referenced more or less directly by other types with one-to-many and many-to-many associations. 相反,我的模型包含许多顶级类,这些类将被具有一对多和多对多关联的其他类型或多或少直接引用。 For this reason I guess that semi-structural, document-based and JSON-based storage (CouchDB, MongoDB) won't suit my needs, as I could not find the way of easily expressing relationships between objects (ie documents) other than aggregation. 出于这个原因,我半结构,基于文档和基于JSON的存储(CouchDB,MongoDB)将无法满足我的需求,因为除了聚合之外,我找不到轻松表达对象(即文档)之间关系的方法。 Or I am just wrong. 还是我错了。
  • Number of persisted objects is going to be 'average', ie there is no need for particular massive scalability, big-data functionalities etc. I'd estimate a number of objects as hundreds of thousands. 持久化对象的数量将是“平均”的,也就是说,不需要特定的大规模可伸缩性,大数据功能等。我估计有数十万个对象。
  • I need the storage to be transactional, with commit on success/rollback on error mechanisms or equivalent. 我需要存储是事务性的,在成功的情况下提交/在错误机制或等效情况下回滚。
  • Schema evolution should not require any operations on underlying storage schema. 模式演变不应该对基础存储模式进行任何操作。 So when my model changes, all I want to do is retrieve object, modify/recalculate properties, and store modified object. 因此,当模型更改时,我要做的就是检索对象,修改/重新计算属性以及存储修改后的对象。 No need to change any tables, columns, etc. 无需更改任何表,列等。
  • I would need to execute some queries based on object properties. 我将需要根据对象属性执行一些查询。 Not really complicated ones, with just regular relational operators, but possibly with joins ( FROM Book b WHERE b.price < :allowedPrice AND b.author=:author ). 并不是真正复杂的,只有常规的关系运算符,但可能有联接( FROM Book b WHERE b.price < :allowedPrice AND b.author=:author )。
  • As it is going to be used in my hobby, eventually-to-become-commercial, project, Id'd like it to be free for commercial use (ideally), or with some non-restrictive licensing for as long as it stays an after-hours thing (and this might be ages ). 由于它将在我的业余爱好项目(最终成为商业项目)中使用,因此我希望它可以免费(理想情况下)用于商业用途,或者只要保持使用期限就可以使用一些非限制性许可。下班后的事情(这可能是年龄 )。

Actually from what I have read, it seems that some object database would be the best choice for me, for example DB4O or ObjectDB. 实际上,从我阅读的内容来看,似乎某些对象数据库对我来说是最佳选择,例如DB4O或ObjectDB。 Unfortunately, DB4O is dead, and license for ObjectDB seems to be a bit too demanding for me. 不幸的是,DB4O已经死了,而ObjectDB的许可证似乎对我来说要求太高了。 Maybe there are some possibilities I am not aware of to fulfill above requirements with something else, some NoSQL solution or some set of tools on top of 'regular' RDBMS. 也许有一些我可能不知道的可能性,可以通过其他方法,NoSQL解决方案或“常规” RDBMS之上的某些工具来满足上述要求。

Do you have any suggestions or advice? 您有什么建议或建议吗?

I decided to focus mainly on object DB solutions, and unfortunately I could not find many of them. 我决定主要关注对象数据库解决方案,但不幸的是我找不到很多。 I evaluated ObjectDB and I have to admit that it's the best persistence solution I happened to work with. 我评估了ObjectDB,我不得不承认这是我碰巧使用过的最佳持久性解决方案。

Pros: 优点:

  • It supports both JPA and JDO APIs, so using OBjectDB is just as easy as using any ORM-based solution. 它支持JPA和JDO API,因此使用OBjectDB与使用任何基于ORM的解决方案一样容易。 If you know JPA or JDO, you have no entry cost for starting with ObjectDB. 如果您知道JPA或JDO,那么从ObjectDB开始就没有入门费用。 If you stick to some really basic guidelines, you can switch between ObjectDB and ORM almost at will, just by changing connection string and deployed JARs. 如果您遵循一些真正的基本准则,则只需更改连接字符串和已部署的JAR,就可以随意在ObjectDB和ORM之间切换。
  • ObjectDB does what almost none of persistence libraries does: it let's you focus on persistence task without any disturbances and inconveniences caused the underlying storage. ObjectDB几乎没有持久性库所能做的:它让您专注于持久性任务,而不会引起基础存储的任何干扰和不便。 You define your model just once, as entity classes, and you do not need to repeat yourself and handle any tables, columns, ORMs ( orm.xml , be gone!), DB schema evolution, moving data between column on schema updates... 您只需将模型定义为实体类一次,就无需重复自己并处理任何表,列,ORM( orm.xml了!),数据库模式演变,在模式更新的列之间移动数据。 。
  • ObjectDB has more relaxed requirements than JPA ORM. 与JPA ORM相比,ObjectDB的要求更为宽松。 You can persist objects of almost any type (GUIDs, new Date and Time API!), you need fewer annotations (for example, relationship annotations, ie @OneToMany , @ManyToMany and others) are not required. 您可以持久保存几乎任何类型的对象(GUID,新的日期和时间API!),不需要的注释(例如,关系注释,即@OneToMany@ManyToMany等)就不需要了。 However you still need them if you need source compatibility between ObjectDB and JPA ORM. 但是,如果需要ObjectDB和JPA ORM之间的源兼容性,则仍然需要它们。

Cons: 缺点:

  • It's a commercial product, and as such it requires you to get a license. 这是一种商业产品,因此需要您获得许可。 It's possible to use ObjectDB for free (commercially or not), but only for up to 10 entity classes. 可以免费使用ObjectDB(无论是否商业),但最多只能使用10个实体类。 Sometimes it might be possible to get free, unlimited license for evaluation purposes - or at least there used to be such information on ObjectDB page, but I do not know if it's possible now - I did not ask. 有时可能有可能出于评估目的获得免费,无限制的许可证-或至少在ObjectDB页面上曾经有这样的信息,但我不知道现在是否有可能-我没有问。
  • While JPA seems to be almost fully supported, static metamodel API is not. 虽然似乎几乎完全支持JPA,但不支持静态元模型API。 There is no static metamodel generator, and even when you'd create it by hand, criteria API methods with metamodel arguments throw Not Supported exception. 没有静态元模型生成器,即使您手动创建它,带有元模型参数的标准API方法也会抛出Not Supported异常。 It might be an obstacle if you'd plan to switch from existing solution (for example, Hibernate) to ObjectDB. 如果您打算从现有解决方案(例如,Hibernate)切换到ObjectDB,这可能是一个障碍。 Hopefully support for static metamodel will be added some time in the future. 希望将来会增加对静态元模型的支持。

For me, ObjectDB seems to be exactly what I need, and I will evaluate it further to check other aspects, as performance, conformance to more obscure corners of JPA and possibility of easy replacement with ORM when needed, schema evolution, etc. 对我来说,ObjectDB似乎正是我所需要的,我将对其进行进一步评估,以检查其他方面,例如性能,对JPA更加晦涩的角落的符合性以及在需要时可以很容易地用ORM替换,模式演变等。

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

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