使用 ORMLite 在数据库中表示字符串列表 [英] Having a list of strings represented in a database using ORMLite

查看:70
本文介绍了使用 ORMLite 在数据库中表示字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是 ORMLite 的新手.我希望我的模型类有一个字段,它是一个字符串列表,它最终会保存我的模型对象的标签列表.我应该使用哪些 ORMLite 注释?

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?

首先我不想有一个包含所有标签的表格,然后使用 @ForeignCollectionField.我也想过使用 @DatabaseField(dataType=DataType.SERIALIZABLE) 注释,但结果证明 List 没有实现 Serializable 界面.

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.

您有什么建议?

推荐答案

首先,List 没有实现 Serializable,但 ArrayList 确实和大多数常见的集合实现一样好.但从纯对象模型的角度来看,存储一个巨大的列表可能不是最好的做法.

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.

那么您为什么不想要一个包含所有标签的表格呢?从纯模型的角度来看,这是最好的方法.如果您每次都需要它们,它将需要第二次查询.这就是 hibernate 存储标签列表或数组的方式.

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.

阅读您的评论@creen 后,我仍然认为您确实想要一个标签表.您的模型类将具有:

After reading your comment @creen, I still think you do want a table of tags. Your model class would then have:

@ForeignCollectionField
Collection<Tag> tags;

tags 表将不会有一个名为 "red" 的标签,其中有多个模型类引用它,但有多个 "红色" 条目.它看起来像:

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:

model_id    name
1           "red"
1           "blue"
2           "red"
3           "blue"
3           "black"

每当您删除模型对象时,您首先要执行一个 tags.clear(); ,它会从标签表中删除与该模型关联的所有标签.您无需进行任何额外的清理工作.

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.

这篇关于使用 ORMLite 在数据库中表示字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆