简体   繁体   English

(ORMLite-android)直接设置外键字段

[英](ORMLite - android) Setting foreign key fields directly

In my android app, I pull down record from the server and save them to the db - I'd like to be able to save "Message" records without having to first save the "Thread" records that they are attached to. 在我的Android应用程序中,我从服务器中提取记录并将其保存到数据库中-我希望能够保存“消息”记录,而无需先保存它们所附加的“线程”记录。

A message is composed of: 消息由以下内容组成:

  • String id 字串编号
  • String body 琴弦琴体
  • Long sent_ts 长send_ts
  • String sender_id 字符串sender_id
  • String topic_id 字符串topic_id

Is it possible to set the topic_id and sender_id records directly on a Message object rather than creating a dummy Topic instance and User instance? 是否可以直接在Message对象上设置topic_id和sender_id记录,而不是创建虚拟的Topic实例和User实例? I know that I can either not mark the fields as foreign and give up on some easier lookups of related objects or save all nested objects first, but is it possible to just directly set the foreign key references in the case that the keys are externally provided? 我知道我不能将字段标记为外部字段,而放弃一些更简单的相关对象查找,或者先保存所有嵌套对象,但是在外部提供键的情况下,可以直接设置外键引用?

This is being done on Android with the standard Android SQLite setup. 这是在Android上使用标准Android SQLite设置完成的。

EDIT 编辑

I finally settled on writing multiple models on the same database table. 我最终决定在同一个数据库表上编写多个模型。 It's not the most elegant solution, but it seemed the only way to be able to either populate the foreign id fields directly or join on other tables, depending on the context. 这不是最优雅的解决方案,但这似乎是能够直接填充外部id字段或联接其他表(取决于上下文)的唯一方法。

I'd like to be able to save "Message" records without having to first save the "Thread" records that they are attached to. 我希望能够保存“消息”记录而不必先保存它们附加到的“线程”记录。

So each Message has a foreign Topic and User fields, and you'd like to persist the Message instances before you've assigned them to those foreign objects? 因此,每个Message都有一个外来的TopicUser字段,您想在将Message实例分配给那些外来对象之前将其持久化吗?

Yes, you can certainly leave your topic and user fields to be null . 是的,您当然可以将topicuser字段保留为null You will need to mark them as canBeNull=true of course. 您当然需要将它们标记为canBeNull=true Then later you should be able to set the topic or user fields on the Message in question and then call messageDao.update(message) to update the database. 之后,您应该能够在有问题的Message上设置topicuser字段,然后调用messageDao.update(message)来更新数据库。

Edit: 编辑:

After reading your comments I now understand. 看完您的评论后,我现在明白了。 Is there a way to just save the id fields onto a class instead of creating dummy objects that just have the id field set? 有没有一种方法可以将id字段仅保存到类上,而不是创建只设置了id字段的虚拟对象? There answer is no unless you do raw SQL create statement yourself. 除非您自己执行原始SQL create语句,否则答案是没有的。 ORMLite needs to have a Topic field because that's what it is expecting. ORMLite需要有一个Topic字段,因为这是期望的。 If you just want the id then you can have a int topic_id field instead of Topic topic and do the topic association yourself. 如果只需要id,则可以使用int topic_id字段代替Topic topic然后自己进行topic关联。

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

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