2017-02-10 52 views
0

我在使用​​和mapping.TableToResourceMapping設置@SearchableComponent對象時遇到問題。ResultSetJdbcGpsDevice&@SearchableComponent

試圖測試,我設置了兩個表映射爲對象(忽略ID的事情,除非這實際上是導致此問題):

a 
id (pk) | name | create_time | update_time 
------------------------------------------ 

b 
id (pk|fk) | property1 | property2 | property3 
---------------------------------------------- 

A和B的定義是這樣的:

@Searchable(root=true) 
public class A { 
    @SearchableId 
    private int id; 

    @SearchableProperty 
    private String name; 

    @SearchableProperty 
    private long create_time; 

    @SearchableProperty 
    private long update_time; 

    @SearchableComponent 
    private B b; 

    // getters, setters 
} 

@Searchable(root=false) 
public class B { 
    @SearchableId 
    private int id; 

    @SearchableProperty 
    private String property1; 

    @SearchableProperty 
    private long property2; 

    @SearchableProperty 
    private String property3; 

    // getters, setters 
} 

下面是我如何設置的一切行動:

conf = new CompassConfiguration() 
    .setSetting(CompassEnvironment.CONNECTION, "target/index") 
    .addClass(A.class) 
    .addClass(B.class); 
compass = conf.buildCompass(); 

ResultSetJdbcGpsDevice jdbc = new ResultSetJdbcGpsDevice(); 
jdbc.setDataSource(ds); 
jdbc.setName("sql-device"); 
jdbc.setMirrorDataChanges(true); 

TableToResourceMapping mappingA = new TabbleToResourceMapping("a", "A"); 
mapping.addIdMapping(new IdColumnToPropertyMapping("id", "id")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("name", "name")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("create_time", "create_time")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("update_time", "update_time")); 

TableToResourceMapping mappingB = new TabbleToResourceMapping("b", "B"); 
mapping.addIdMapping(new IdColumnToPropertyMapping("id", "id")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("property1", "property1")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("property2", "property2")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("property3", "property3")); 

jdbc.addMapping(mappingA); 
jdbc.addMapping(mappingB); 

gps = new SingleCompassGps(compass); 
gps.addGpsDevice(jdbc); 
gps.start(); 
gps.index(); 

有了這個,我得到以下錯誤,從指南針:

java.lang.IllegalStateException: {sql-device}: No resource mapping defined in gps mirror compass for alias [B]. Did you defined a jdbc mapping builder? 
at org.compass.gps.device.jdbc.ResultSetJdbcGpsDevice.doStart(ResultSetJdbcGpsDevice.java:127) 
at org.compass.gps.device.AbstractGpsDevice.start(AbstractGpsDevice.java:124 
at org.compass.gps.impl.AbstractCompassGps.start(AbstractCompassGps.java:166) 
... 

如果我集B爲root=true它工作正常,我可以在A和B字段搜索文本和得到的東西回來。但是,當A被構建並通過搜索返回時,其B實例是空的。

如果我搜索property2:0然後我檢索B的實例映射到該列爲0的行,但我也檢索A的每個實例 - 因爲它們的所有B組件都是默認實例。

我該如何正確設置它?我覺得我可能會錯過Compass文檔中的一些東西。

回答

0

試圖弄清楚這一點後,我制定了以下解決方案:

TableToResourceMapping mapping = new TabbleToResourceMapping("a", "A"); 
mapping.setQuery("select * from a join b using (id)"); 

mapping.addIdMapping(new IdColumnToPropertyMapping("id", "id")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("name", "name")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("create_time", "create_time")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("update_time", "update_time")); 

mapping.addDataMapping(new DataColumnToPropertyMapping("property1", "property1")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("property2", "property2")); 
mapping.addDataMapping(new DataColumnToPropertyMapping("property3", "property3")); 

jdbc.addMapping(mapping); 

這可能不是做正確的方式,但它的作品。搜索給我提供了一個實例A,它的字段中有正確的數據,包括一個實例B也有正確的數據。