ORMLite 外来 @DatabaseField 具有逆向映射?
我正在实现一个小项目,我想知道 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 @DatabaseMapping
s. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白这将如何运作@ilikeorangutans。我假设一个作者有多个博客文章?那么帐户频道上怎么可能只有一个
blogpost_id
呢?您可以有一个包含author_id
和blogpost_id
的联接表,但这只会增加复杂性。如果您查看此讨论和此网页,可以看到Hibernate的逆映射是关于谁控制关系并负责更新行。在这两种情况下,
BlogPost
都会有一个author_id
,而不是相反。ORMLite 确实支持“外部集合”的概念,您可以在其中添加
通过将 BlogPost
添加到Author
对象中的集合中,将其添加到数据库。有关详细信息,请参阅外部集合文档。抱歉,如果我遗漏了什么。
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 anauthor_id
and ablogpost_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 anauthor_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 theAuthor
object. For more information see the foreign collection documentation.Sorry if I'm missing something.