简体   繁体   English

如何使用JPA保留复杂的嵌套对象

[英]How to persist a complicated, nested object with JPA

I'm working on a project which parses information with a NLP library from classified ads on internet forums. 我正在一个项目,该项目使用NLP库从互联网论坛上的分类广告中解析信息。 After the processing I have ForSaleItem instances and all sorts of details like condition, price etc. 处理之后,我得到了ForSaleItem实例和各种详细信息,例如条件,价格等。

When I call the toString() method on these objects, it does not return a string stored in the object itself but rather looks up its own start and end point within the entire forum post and then extracts the text from there. 当我在这些对象上调用toString()方法时,它不会返回存储在对象本身中的字符串,而是在整个论坛帖子中查找其自己的起点和终点,然后从那里提取文本。 (This is done so I can calculate proximity of individual bits of text). (这样做是为了我可以计算文本的各个位的接近度)。

I would like to use JPA to persist these objects to a RDBMS. 我想使用JPA将这些对象持久保存到RDBMS。 After the processing I don't really care about the proximity stuff anymore, I just want to display the strings to the user. 处理完之后,我不再真正关心邻近项,我只想向用户显示字符串。 It would be an enormous waste if I'd persist the entire forum post and keep retrieving the actual text through the above method. 如果我坚持整个论坛的帖子,并继续通过上述方法检索实际文本,那将是一个巨大的浪费。

My question now: Should I enhance the original classes with String fields or should I create an entirely new class, ie. 现在我的问题是:我应该使用String字段增强原始类,还是应该创建一个全新的类,即。 something like PersistentForSaleItem? 像PersistentForSaleItem之类的东西?

I would recommend creating a new item, that persists and contains only the pieces you are concerned with. 我建议创建一个新项目,该项目将持续存在,并且仅包含您关注的部分。 This seems like it might be easiest, and would allow you to separate the concerns of gathering/manipulating the data and the final storage and display of the data. 这似乎是最简单的方法,它可以使您分离收集/处理数据以及最终存储和显示数据的问题。 You could even have the persistent objects be created by passing in the other object during construction, and then use annotations/xml configurations to persist the object into the RDBMS. 您甚至可以通过在构造过程中传入另一个对象来创建持久对象,然后使用批注/ xml配置将该对象持久保存到RDBMS中。

Just my 2 cents. 只是我的2美分。

Looks like your ForSaleItem have several points really incompatible with what you want to persist : 看起来您的ForSaleItem有几点确实与您要保留的内容不兼容:

  • complex retrieving process of the String 字符串的复杂检索过程

    -> unneeded after persistence. ->持久后不需要。

  • multiple objets that would be persisted to multiples tables 多个对象,这些对象将保留到多个表中

    -> unnecessary complex and slow, while with only one objet you can store the String in a single table, and retrieve it more efficiently. ->不必要的复杂且缓慢,而只有一个对象可以将String存储在单个表中,并更有效地检索它。

Therefore, I advise you use a different object for Persistence. 因此,我建议您对持久性使用不同的对象。

Note that it can be really simple to create, for example : 请注意,创建起来可能非常简单,例如:

  • a class, no superclass, with annotation @Entity 一个类,没有超类,带有注释@Entity
  • an Long identifier field, with annotations @Id and @GeneratedValue 一个长标识符字段,带有@Id和@GeneratedValue批注
  • one String field, with getter (with @Column if you want a String length maximum different from the 256 default) and setter 一个String字段,带有getter(如果想要的最大字符串长度与256个默认值不同,则使用@Column)和setter
  • an empty constructor (optional) 一个空的构造函数(可选)

  • - --

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

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