使用 ORMLite 在数据库中表示字符串列表
首先我是 ORMLite 的新手。我希望我的模型类有一个字符串列表字段,它最终会保存我的模型对象的标签列表。 我应该使用哪些 ORMLite 注释?
首先,我不想拥有所有标签的表格,然后使用@ForeignCollectionField
。 我还想过使用 @DatabaseField(dataType=DataType.SERIALIZABLE) 注释,但事实证明 List
没有实现 Serializable< /代码> 接口。
您有什么建议?
First of I am new to ORMLite. I would like my model class to have a field which is a list of strings, that would eventually hold a list of tags for my model object.
Which ORMLite annotations should I use?
Firstly I don't want to have a table of all tags, and then use the @ForeignCollectionField
.
Also I thought of using the @DatabaseField(dataType=DataType.SERIALIZABLE)
annotation, but it turns out that List<String>
doesn't implement the Serializable
interface.
What are your suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,List 没有实现
Serialized
,但 ArrayList 确实实现了以及大多数常见的集合实现。但从纯对象模型的角度来看,存储一个巨大的列表可能并不是最好的方法。那么为什么你不想拥有一个包含所有标签的表格呢?从纯模型的角度来看,这是最好的方法。如果您每次都需要它们,则需要第二次查询。这就是 hibernate 存储标签列表或数组的方式。
阅读您的评论 @creen 后,我仍然认为您确实想要一个标签表。然后,您的模型类将具有:
tags
表不会有一个名为"red"
的单个标记,多个模型类引用它,但多个“红色”
条目。它看起来像:每当您删除模型对象时,您首先会执行
tags.clear();
,这将从标签表中删除与该模型关联的所有标签。您无需进行任何额外的清理或任何操作。First of all, List doesn't implement
Serializable
but ArrayList certainly does as well as most of the common collection implementations. But storing a huge list is probably not the best of doing this from a pure object model standpoint.So why don't you want to have a table of all tags? That's the best way from a pure model standpoint. It will require a 2nd query if you need them every time. That's the way hibernate would store a list or array of tags.
After reading your comment @creen, I still think you do want a table of tags. Your model class would then have:
The
tags
table would not have a single tag named"red"
with multiple model classes referring to it but multiple"red"
entries. It would look like:Whenever you are removing the model object, you would first do a
tags.clear();
which would remove all of the tags associated with that model from the tags table. You would not have to do any extra cleanup or anything.无需为简单的字符串数组而使用 @ForeignCollectionField
将代码更改
为
数据库不想存储可动态增长的数组。这就是它只允许像 string[] 这样的静态数组而不是 List 的原因。
No need to go for @ForeignCollectionField for simple String Array
Change your code
to
Database doesn't want to store dynamically grow able arrays. That is the reason it allows only static array like string[] and not List.
我添加了两个属性...一个作为 csv 字符串写入数据库,另一个对此进行翻译:
I added two properties... one that gets written to the database as a csv string and the other that translates this: