2

I have two classes. User and Address.

User:

    @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;

}

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;
}

I can get results for direct Address query as follows:

  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();

But when I try same query with User.class, it does not return me any results. With index embedded I should be getting it directly or do I need to change anything?

rishi
  • 2,564
  • 6
  • 25
  • 47

1 Answers1

0

Add @Spatial annotation to the address field of the User entity:

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