Hibernate搜索空间嵌入式索引查询

时间:2014-09-18 13:16:01

标签: hibernate hibernate-search hibernate-spatial

我有两节课。用户和地址。

用户:

    @Indexed
    @Entity
    @Table(name = "USERS")
    public class USER {

        @DocumentId
        @Id
        @Column(name = "ID")
        @GeneratedValue(strategy = GenerationType.TABLE, generator = "userSeq")
        @TableGenerator(name = "userSeq", table = "SEQUENCES", pkColumnName = "SEQ_NAME", valueColumnName = "NEXT_VALUE", pkColumnValue = "USER_SEQ", initialValue = 1, allocationSize = 1)
        private Long id;

        @Field
        @Column(name = "NAME", length = 255)
        private String name;

        @IndexedEmbedded(depth = 1)
        @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
        @JoinColumn(name = "ADDRESS_ID")
        private Address address;

}

地址:

@Indexed
@Entity
@Spatial
@Table(name = "ADDRESSES")
public class Address {

    @DocumentId
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "addSeq")
    @TableGenerator(name = "addtSeq", table = "SEQUENCES", pkColumnName = "SEQ_NAME", valueColumnName = "NEXT_VALUE", pkColumnValue = "ADD_SEQ", initialValue = 1, allocationSize = 1)
    private Long id;

    @Field
    @Column(name = "NAME", length = 255)
    private String name;

    @Latitude
    @Column(name = "LAT")
    private Double latitude;

    @Longitude
    @Column(name = "LON")
    private Double longitude;

    @OneToMany(mappedBy = "address", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<User> users;
}

我可以获得直接地址查询的结果,如下所示:

  FullTextSession fullTextSession = Search.getFullTextSession(session);
            QueryBuilder builder = fullTextSession.getSearchFactory()
                    .buildQueryBuilder().forEntity(Address.class).get();

            org.apache.lucene.search.Query luceneQuery = builder.spatial()
                    .onDefaultCoordinates().within(radius, Unit.KM).ofLatitude(lat)
                    .andLongitude(lon).createQuery();


            Transaction tx = session.beginTransaction();

            org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(
                    luceneQuery, Address.class);
            hibQuery.list();

但是当我用User.class尝试相同的查询时,它不会返回任何结果。嵌入索引我应该直接获取它还是需要更改任何内容?

1 个答案:

答案 0 :(得分:0)

在用户实体的@Spatial字段中添加address注释:

@Spatial
@IndexedEmbedded(depth = 1)
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
@JoinColumn(name = "ADDRESS_ID")
private Address address;