ORMLite 外来 @DatabaseField 具有逆向映射?

发布于 2024-11-14 11:16:58 字数 614 浏览 2 评论 0原文

我正在实现一个小项目,我想知道 ORMLite 是否支持 @DatabaseMapping 的逆映射。我正在寻找类似于 JPA/Hibernates 的逆映射。下面是一个假设的相当愚蠢的例子,一个表 BlogPost

@DatabaseTable
public class BlogPost {
  @DatabaseField(foreign = true)
  private Author owner;
}

和相应的 Author 类,并不是那么重要:

@DatabaseTable public class Author { }

这会产生以下 SQL(只是相关部分):

<代码> 创建表博客文章(...,owner_id INTEGER NOT NULL,...) 创建表作者 ( ... )

查看表 blogpost 现在如何具有作者的外键。但是,我更喜欢相反的方式,即作者应该有一个 blogpost_id 外键。 (我告诉过你这是一个愚蠢的例子......;)。

通过逆映射,我可以利用级联进行删除,但我在 ORMlite 文档中没有找到任何关于此的内容。这不是一个功能还是我只是错过了一些东西?

I'm implementing a small project and I'm wondering if ORMLite supports inverse mapping for @DatabaseMappings. What I am looking for is this similar to JPA's/Hibernates's inverse mapping. Following, hypothetical and rather silly example, a table BlogPost:

@DatabaseTable
public class BlogPost {
  @DatabaseField(foreign = true)
  private Author owner;
}

and the according Author class, not really that important:

@DatabaseTable public class Author { }

This results in the following SQL (just the relevant parts):


CREATE TABLE blogpost ( ... , owner_id INTEGER NOT NULL, ... )
CREATE TABLE author ( ... )

See how table blogpost now has a foreign key for author. However, I'd prefer it the other way around, i.e. author should have a blogpost_id foreign key. (I told you it was a silly example... ;).

With inverse mapping I could utilize cascades for deletes but I haven't found anything in the ORMlite docs about this. Is it not a feature or am I just missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

人生戏 2024-11-21 11:16:58

我对这个问题有点困惑,但我想我还是会尝试回答它。

我不明白这将如何运作@ilikeorangutans。我假设一个作者有多个博客文章?那么帐户频道上怎么可能只有一个 blogpost_id 呢?您可以有一个包含 author_idblogpost_id 的联接表,但这只会增加复杂性。

如果您查看此讨论和此网页,可以看到Hibernate的逆映射是关于谁控制关系并负责更新行。在这两种情况下,BlogPost 都会有一个 author_id,而不是相反。

ORMLite 确实支持“外部集合”的概念,您可以在其中添加 通过将 BlogPost 添加到 Author 对象中的集合中,将其添加到数据库。有关详细信息,请参阅外部集合文档

抱歉,如果我遗漏了什么。

I'm a bit confused by the question but I thought I'd try to answer it anyway.

I don't understand how that would work @ilikeorangutans. I assume that there are multiple blogposts for a single author? So how could there be a single blogpost_id on the account channel? You could have a join table which contains an author_id and a blogpost_id but that just adds complexity.

If you take a look at this discussion and this webpage, you can see that Hibernate's inverse mapping is about who controls the relationship and is responsible for updating the rows. In both cases, BlogPost would have an author_id and not the other way around.

ORMLite does support the concept of "foreign collections" where you can add a BlogPost to the database by adding it to the collection in the Author object. For more information see the foreign collection documentation.

Sorry if I'm missing something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文