使用 ORMLite 在数据库中表示字符串列表

发布于 2024-11-06 23:16:53 字数 308 浏览 0 评论 0原文

首先我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲鸠 2024-11-13 23:16:53

首先,List 没有实现 Serialized,但 ArrayList 确实实现了以及大多数常见的集合实现。但从纯对象模型的角度来看,存储一个巨大的列表可能并不是最好的方法。

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


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

@ForeignCollectionField
Collection<Tag> tags;

tags不会有一个名为 "red" 的单个标记,多个模型类引用它,但多个“红色” 条目。它看起来像:

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

每当您删除模型对象时,您首先会执行 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:

@ForeignCollectionField
Collection<Tag> tags;

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"

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.

无法言说的痛 2024-11-13 23:16:53

无需为简单的字符串数组而使用 @ForeignCollectionField

将代码更改

@DatabaseField(dataType=DataType.SERIALIZABLE) 
List<String> users;

@DatabaseField(dataType = DataType.SERIALIZABLE)
String[] users;

数据库不想存储可动态增长的数组。这就是它只允许像 string[] 这样的静态数组而不是 List 的原因。

No need to go for @ForeignCollectionField for simple String Array

Change your code

@DatabaseField(dataType=DataType.SERIALIZABLE) 
List<String> users;

to

@DatabaseField(dataType = DataType.SERIALIZABLE)
String[] users;

Database doesn't want to store dynamically grow able arrays. That is the reason it allows only static array like string[] and not List.

南烟 2024-11-13 23:16:53

我添加了两个属性...一个作为 csv 字符串写入数据库,另一个对此进行翻译:

[Ignore]
public List<string> Roles
{
    get
    {
        return new List<string>(RolesCsv.Split(new char[] { ',' }));
    }
    set
    {
        RolesCsv = string.Join(",", value);
    }
}
public string RolesCsv { get; set; }

I added two properties... one that gets written to the database as a csv string and the other that translates this:

[Ignore]
public List<string> Roles
{
    get
    {
        return new List<string>(RolesCsv.Split(new char[] { ',' }));
    }
    set
    {
        RolesCsv = string.Join(",", value);
    }
}
public string RolesCsv { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文