How to use spatial annotation?

2019-08-27 02:37发布

问题:

I am using elastic search with hibernate search.I am having a requirement where I have to build tiles map in kibana.For that I need to have geo_point data with latitude and longitude value.I found in hibernate we have @spatial annotation to do this,but when I am using I am getting below exception.

java.lang.NullPointerException at org.hibernate.search.engine.metadata.impl.TypeMetadata$Builder.getAnalyzerReference(TypeMetadata.java:631)

I am having two model entity and I am writing it below.I am using hibernate search 5.7.0.Final.

@Entity
@Table(name = "actors", catalog = "twitter")
public class Actors implements java.io.Serializable {
    Set<ActorsLocation> actorsLocation;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "actors")
    @Cascade(CascadeType.SAVE_UPDATE)
    @IndexedEmbedded
    public Set<ActorsLocation> getActorsLocation() {
        return actorsLocation;
    }
    public void setActorsLocation(Set<ActorsLocation> actorsLocation) {
        this.actorsLocation = actorsLocation;
    }
}
@Entity
@Spatial
@Table(name = "actors_location", catalog = "twitter")
public class ActorsLocation implements java.io.Serializable {
    private static final long serialVersionUID = 3585685166633868737L;
    Long id;
    @Id
    @Column(name = "id",nullable = false)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Latitude
    @Field
    Double lat;
    @Longitude
    @Field
    Double lon;
    @ContainedIn
    Actors actors;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)  
    public Actors getActors() {
        return actors;
    }
    public void setActors(Actors actors) {
        this.actors = actors;
    }
}

Any suggestion how to resolve it?

回答1:

As mentioned in my comment, the NPE is a bug: https://hibernate.atlassian.net/browse/HSEARCH-2858

We should fix it fairly quickly, but even if the bug above gets fixed, another one will still remain and prevent you from storing multi-valued locations (multiple "ActorLocations" for a single "Actor"): https://hibernate.atlassian.net/browse/HSEARCH-2859 . That bug is unfortunately harder to fix, we may need to wait for a major version.

One way to work around both issues would be to reverse index embedding: embed the actor in your ActorLocations and not the opposite. Then, execute all your search queries against ActorLocations and not against Actor. Not an ideal solution, I know, but it should work.