获取现有实体(如果存在)或创建一个新实体
我正在导入的数据可能已存在于我的数据库中,也可能不存在。我希望 NHibernate 将任何实体与现有数据库实体关联(如果存在)(可能只是设置主键/id),或者创建一个新实体(如果不存在)。我的框架使用 S#arp 架构(MVC 2、NHibernate、Fluent)。
我已将 [HasUniqueDomainSignature] 属性添加到该类,并将 [DomainSignature] 属性添加到我想要用于比较的属性。我能想到的唯一方法(这不是一个可接受的解决方案,甚至可能不起作用)如下(伪 C#):
foreach (Book importedBook in importedBooks){
foreach (Author author in importedBook.Authors){
if (!author.IsValid()){ // NHibernate Validator will check DomainSignatures
author = _authorRepository.GetByExample(author); // This would be to get the db object with the same signature,
//but I don't think I could even update this as I iterate through it.
}
}
}
正如您所看到的,这既混乱又无意义。除此之外,我对这本书有六个联想(主题、格式等),但它没有任何意义。一定有一种我缺少的简单方法可以做到这一点。我不是 NHibernate 的新手,但我绝对不是专家。
I'm importing data that may or may not exist already in my database. I'd like NHibernate to associate any entities with the existing db one if it exists (probably just setting the primary key/id), or create a new one if it doesn't. I'm using S#arp architecture for my framework (MVC 2, NHibernate, Fluent).
I've added the [HasUniqueDomainSignature] attribute to the class, and a [DomainSignature] attribute to the properties I want to use for comparison. The only way I can think to do it (which is not an acceptable solution and may not even work) is the following (psuedo C#):
foreach (Book importedBook in importedBooks){
foreach (Author author in importedBook.Authors){
if (!author.IsValid()){ // NHibernate Validator will check DomainSignatures
author = _authorRepository.GetByExample(author); // This would be to get the db object with the same signature,
//but I don't think I could even update this as I iterate through it.
}
}
}
As you can see, this is both messy, and non-sensical. Add to that the fact that I've got a half dozen associations on the Book (subject, format, etc), and it doesn't make any sense. There's got to be an easy way to do this that I'm missing. I'm not a novice with NHibernate, but I'm definitely not an expert.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能不明白这个问题,但是数据如何“可能存在于数据库中,也可能不存在”?例如,如果一本书有 2 个作者,如果作者不存在,那么如何在数据库级别存储关系?
看起来好像您正在尝试使用 NHibernate 导入数据(或者创建一个实体,如果它不存在),这似乎不正确。
I might not be understanding the problem, but how can the data "may or may not exist in the database"? For example, if a Book has 2 Authors, how is the relationship stored at the database level if the Author doesn't exist?
It seems as if you're trying to use NHibernate to import your data (or create an entity if it doesn't exist) which doesn't seem correct.
大多数数据库实现都支持条件 UPDATE-or-INSERT 语法。例如,Oracle 有一个 MERGE 命令。结合映射中的 Hibernate
块,您应该能够解决问题。我不知道 Fluent,但我认为它也支持这一点。Most database implementations support a conditional UPDATE-or-INSERT syntax. Oracle, for example, has a MERGE command. In combination with a Hibernate
<sql-insert>
block in your mapping you should be able to work something out. I don't know Fluent but I assume it supports this too.只是意识到我从未给出过答案或批准了别人的答案。我最终只是编写了一个新的 SaveOrUpdate ,它需要一个参数来在持久化之前检查是否存在。我还在域模型中添加了一个属性,以便在保存/更新时进行覆盖(尽管回想起来,只有在更新时才会覆盖)。
这是代码,如果它可以帮助其他人解决这个困境:
Just realize I never gave an answer or approved another's answer. I ended up just writing a new SaveOrUpdate which takes a parameter to check for existing before persisting. I also added an attribute to my domain models to overwrite when saving/updating (although in retrospect it's only on updating that it'd be overwriting).
Here's the code if it can help anyone else in this dilemma: