休眠搜索 - 自定义桥值不更新 [英] Hibernate Search - Custom Bridge value not updating

查看:129
本文介绍了休眠搜索 - 自定义桥值不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自:按子女计数的Hibernate搜索顺序
我有:

  @Indexed 
@Entity
public class TParent implements java.io.Serializable {

.....
私人套餐< TChild> TChildSet = new HashSet< TChild>(0);

@ContainedIn
@FieldBridge(impl = CollectionCountBridge.class)
@Field(analyze = Analyze.NO)
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy =TParent)
public Set< TChild> getTChildSet(){
return this.TChildSet;
}

  @Indexed 
@Entity
公共类TChild实现java.io.Serializable {

.....
private TParent TParent;

@IndexedEmbedded
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =parent_id,nullable = false)
public TParent getTParent(){
返回this.TParent;
}

感谢Don Roby,我已经应用了这个CustomBridge

  public class CollectionCountBridge extends IntegerBridge {

@Override
public String objectToString(Object object){
if(object == null ||(!(object instanceof Collection))){
return null;
}
集合<?> coll =(Collection<>)对象;
int size = coll.size();
System.out.println(col.size():+ size);
返回super.objectToString(size);
}

}

我列出了TParents。我试图通过TChildSet计数来排列它们。
当我首先创建索引时,它完美地工作。我可以列出TParent,并按TChild的数量排序。


$ b $ Full $ Text FullTextQuery =类);
fullTextQuery.setSort(new Sort(new SortField(TChildSet,SortField.INT,true)));

现在...



当我添加一个新的TChild,执行CustomBridge代码。我希望看到订单更改,因为我添加了TChild的。



我可以看到TChild计数增加了(按照CustomBridge代码中的System.out.println())。

但是,查询没有正确排序。索引首次构建时的原始排序顺序依然存在。添加新的TChild对排序顺序没有影响。
我猜索引没有被更新,但我不确定。



任何帮助或指针都会很棒。



非常感谢

约翰



编辑
直接的问题是该值没有在索引中更新。这可以使用卢克。

解决方案

问题



在实体中,对非DB表字段的索引字段执行更新,则保存实体不会导致索引更新。 (将实体保存为对数据库表字段的索引字段进行更新将自动导致索引更新)。



解决方案

 public static void addTChild() {
Session session = null;
尝试{
session = HibernateUtil.getSession();
session.beginTransaction();
TParent tParent =(TParent)session.get(TParent.class,6L);
TChild tChild = new TChild(tParent,Auto added+ new Date()。toString(),'y');
Long id =(Long)session.save(tChild);
tChild =(TChild)session.get(TChild.class,id);
tParent.getTChildSet()。add(tChild);
session.save(tParent);

/ *
*手动更新索引。
*由于事实上索引字段更新
*不是数据库表字段,因此不会导致索引更新
* /
FullTextSession fullTextSession = Search.getFullTextSession(会议);
fullTextSession.index(tParent);

session.getTransaction()。commit();
} catch(Exception ex){
session.getTransaction()。rollback();
ex.printStackTrace();
} finally {
if(session!= null){
session.close();



code
$ b

实体:

  / * 
* TParent
* /
@ContainedIn
@FieldBridge(impl = CollectionCountBridge.class)
@Field(analyze = Analyze.NO,store = Store.YES,name =cCount)
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = TParent)
public List< TChild> getTChildSet(){
return this.TChildSet;



<$
* TChild
* /
@IndexedEmbedded
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( name =parent_id,nullable = false)
public TParent getTParent(){
return this.TParent;
}

这就是我所能确定的。


From: Hibernate Search Order by child-count I've got:

@Indexed
@Entity
public class TParent  implements java.io.Serializable {

.....
private Set<TChild> TChildSet = new HashSet<TChild>(0);

@ContainedIn
@FieldBridge(impl = CollectionCountBridge.class)
@Field(analyze = Analyze.NO)
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="TParent")
public Set<TChild> getTChildSet() {
   return this.TChildSet;
}

and

@Indexed
@Entity
public class TChild  implements java.io.Serializable {

.....
private TParent TParent;

@IndexedEmbedded
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="parent_id", nullable=false)
public TParent getTParent() {
    return this.TParent;
}

and thanks to Don Roby, I've applied this CustomBridge

public class CollectionCountBridge extends IntegerBridge {

   @Override
   public String objectToString(Object object) {
     if (object == null || (!(object instanceof Collection))) {
         return null;
     }
     Collection<?> coll = (Collection<?>) object;
     int size = coll.size();
     System.out.println("col.size(): " + size);
     return super.objectToString(size);
  }

}

I'm listing TParents. I'm trying to order them by the TChildSet count. It works perfectly when I build the index first. I can list TParent's and order them by the by the number of TChild's.

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, TParent.class);
fullTextQuery.setSort(new Sort(new SortField("TChildSet", SortField.INT, true)));

Now...

When I add a new TChild, the CustomBridge code is executed. I'd expect to see the ordering change as I add TChild's.

I can see that the TChild count is increased (as per the System.out.println() in the CustomBridge code).

However, the query does not order them correctly. The original sort order from when the index was first built remains. Adding new TChild's has no effect on the sort order. I guess the index is not being updated but I'm not sure.

Any help or pointers would be great.

Thanks Very Much

John

Edit The immediate problem is that the value is not being updated in the index. This can be seen using Luke.

解决方案

The Problem

In an Entity, performing an update to an index field that is not a DB table field then saving the Entity will not result in an index update. (Saving the Entity with an update to an index field that is a DB table field will cause an index update automatically).

Solution

(Simply) Manually update the index

public static void addTChild() {
    Session session = null;
    try {
        session = HibernateUtil.getSession();
        session.beginTransaction();
        TParent tParent = (TParent)session.get(TParent.class, 6L);
        TChild tChild = new TChild(tParent, "Auto added " + new Date().toString(), 'y');
        Long id  = (Long)session.save(tChild);
        tChild = (TChild)session.get(TChild.class, id);
        tParent.getTChildSet().add(tChild);
        session.save(tParent);

        /*
         * Manually update the index.
         * Necessary due to fact that the index field updated
         * is not a DB table field and hence will not cause an index update
         */
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        fullTextSession.index(tParent);

        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        ex.printStackTrace();
    } finally{
        if(session != null) {
            session.close();
        }
    }
}

The Entities:

/*
 * TParent
 */
@ContainedIn
@FieldBridge(impl = CollectionCountBridge.class)
@Field(analyze = Analyze.NO, store = Store.YES, name = "cCount")
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="TParent")
public List<TChild> getTChildSet() {
    return this.TChildSet;
}

and:

/*
 * TChild
 */
 @IndexedEmbedded
 @ManyToOne(fetch=FetchType.LAZY)
 @JoinColumn(name="parent_id", nullable=false)
 public TParent getTParent() {
    return this.TParent;
 }

And that's it as far as I can determine.

这篇关于休眠搜索 - 自定义桥值不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆