NHibernate.Search + Lucene.NET 重复条目
当我从现有数据库构建初始索引时,Lucene 中的条目类型被索引为 <_hibernate_class:Castle.Proxies.MuestraProxy, DynamicProxyGenAssembly2>
但是,当使用 NHibernate 添加新实体时,它会被索引为 <_hibernate_class:RALVet.Model.Muestra, RALVet>} ,这会在 Lucene 中创建重复条目,一个作为真实类,另一个作为它的代理。
我在构建索引时尝试使用 NHibernateUtil.GetClass() 但它仍然返回代理。
这是构建初始索引的代码:
private static void CreateIndex<T>()
{
var fullTextSession =
NHibernate.Search.Search.CreateFullTextSession(BootStrapper.SessionFactory.OpenSession());
using(var tran = fullTextSession.BeginTransaction())
{
var query = fullTextSession.CreateQuery(string.Concat("from ", typeof (T).Name));
foreach (var doc in query.List())
{
fullTextSession.Index(doc);
// fullTextSession.Index(NHibernateUtil.GetClass(doc));
}
tran.Commit();
}
fullTextSession.Close();
}
When I build the initial index from an existing database, the type of the entries in Lucene get indexed as <_hibernate_class:Castle.Proxies.MuestraProxy, DynamicProxyGenAssembly2>
However, when a new entity is added using NHibernate it gets indexed as <_hibernate_class:RALVet.Model.Muestra, RALVet>} which creates duplicate entries in Lucene, one as the real class and another one as its proxy.
I've tried using NHibernateUtil.GetClass() when building the index but it still returns the proxy.
This is the code that builds the initial index:
private static void CreateIndex<T>()
{
var fullTextSession =
NHibernate.Search.Search.CreateFullTextSession(BootStrapper.SessionFactory.OpenSession());
using(var tran = fullTextSession.BeginTransaction())
{
var query = fullTextSession.CreateQuery(string.Concat("from ", typeof (T).Name));
foreach (var doc in query.List())
{
fullTextSession.Index(doc);
// fullTextSession.Index(NHibernateUtil.GetClass(doc));
}
tran.Commit();
}
fullTextSession.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑2:嗯,我对SO进行了另一次搜索,我认为你应该看看 这个答案。我无法重现您的问题,所以要么这里有其他东西在起作用(您的配置或域模型?),要么我完全没有抓住要点。祝你好运!
我将尝试猜测这里的原因(我不使用 C# 或 hql,所以如果它是这个领域的东西,我可能会运气不好)。我认为您所看到的是因为您使用了
.List()
方法的无类型版本。您应该尝试使用.List()
来指定列出实体时所需的类型。我认为
可以解决问题。
编辑:好的,显然它不适用于添加的
(显然代码片段吃了括号,所以如果这样做,请确保复制粘贴正确的版本)。FWIW,我们在工作中所做的事情如下。我们使用 UnitOfWork 模式,因此 UnitOfWork 启动当前的 Nhibernate 配置和会话。我使用 Reflector 从 Vb.net 转到 c#
我将尝试以更精简的方式重现您的问题
Edit 2: Well, i did another search on SO and i think that you should take a look on this answer. I can't reproduce your problem, so either there's something else at play here (your config or domain models?) or i'm completely missing the point. Good luck!
I'm going to try and guess at the reason here (i don't use C# or the hql, so if it's something in this area, i may be out of luck). I think what you see is because you use the typeless version of the
.List()
method. You should try and use.List<T>()
that will specify the type you expect when listing your entities.I think that
should do the trick.
Edit: ok so apparently it's not working with the added
<T>
(apparently the code snippet ate the brackets, so make sure you copy-pasted the right version if you did so).FWIW, what we're doing at work is below. We use the UnitOfWork pattern so the UnitOfWork embarks the current Nhibernate configuration and session. I used Reflector to go from Vb.net to c#
I'm going to try and reproduce you problem in a leaner way
好吧,我终于让它完美运行了。
问题是我使用拦截器在我的实体中注入 INotifyPropertyChanged 实现。删除拦截器使 NH.Search 和 Lucene.net 能够很好地工作。
Ok, I finally got it working perfectly.
The problem was that I was using an interceptor to inject the INotifyPropertyChanged implementation in my entities. Removing the interceptor made NH.Search and Lucene.net to work nicely.