Skip to content

Commit

Permalink
HSEARCH-923 @embeddable with @spatial annotation test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Helleringer authored and Sanne committed Jun 21, 2012
1 parent 077914d commit 963e51a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
@@ -0,0 +1,37 @@
package org.hibernate.search.test.spatial;

import javax.persistence.Embeddable;

import org.hibernate.search.annotations.Spatial;
import org.hibernate.search.spatial.Coordinates;

/**
* Created with IntelliJ IDEA.
* User: nicolashelleringer
* Date: 30/05/12
* Time: 16:50
* To change this template use File | Settings | File Templates.
*/
@Embeddable
public class Position {
String address;
double latitude;
double longitude;

@Spatial
public Coordinates getLocation() {
return new Coordinates() {
@Override
public Double getLatitude() {
return latitude;
}

@Override
public Double getLongitude() {
return longitude;
}
};
}

public Position() {}
}
@@ -0,0 +1,63 @@
package org.hibernate.search.test.spatial;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Spatial;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.spatial.Coordinates;

import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;

/**
* Hibernate Search spatial : Point Of Interest test entity
*
* @author Nicolas Helleringer <nicolas.helleringer@novacodex.net>
*/
@Entity
@Indexed
public class Restaurant {
@Id
Integer id;

@Field(store = Store.YES)
String name;

@IndexedEmbedded
Position position;

public Restaurant(Integer id, String name, String address,double latitude, double longitude) {
this.id = id;
this.name = name;
this.position= new Position();
this.position.address= address;
this.position.latitude = latitude;
this.position.longitude = longitude;
}

public Restaurant() {
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public String getAddress() {
return position.address;
}

public Double getLatitude() {
return position.latitude;
}

public Double getLongitude() {
return position.longitude;
}

}
Expand Up @@ -277,14 +277,55 @@ public void testSpatialAnnotationOnClassLevelRangeMode() throws Exception {
fullTextSession.close();
}

public void testSpatialAnnotationOnEmbeddableFieldLevel() throws Exception {
Restaurant restaurant = new Restaurant( 1, "Al's kitchen", "42, space avenue CA8596 BYOB Street", 24.0d, 32.0d);
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );

Transaction tx = fullTextSession.beginTransaction();
fullTextSession.save( restaurant );
tx.commit();

tx = fullTextSession.beginTransaction();
double centerLatitude= 24;
double centerLongitude= 31.5;

org.apache.lucene.search.Query luceneQuery = SpatialQueryBuilder.buildSpatialQueryByRange(
centerLatitude,
centerLongitude,
50,
"position.location"
);
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, Restaurant.class );
List results = hibQuery.list();
Assert.assertEquals( 0, results.size() );

org.apache.lucene.search.Query luceneQuery2 = SpatialQueryBuilder.buildSpatialQueryByRange(
centerLatitude,
centerLongitude,
51,
"position.location"
);
org.hibernate.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, Restaurant.class );
List results2 = hibQuery2.list();
Assert.assertEquals( 1, results2.size() );

List<?> events = fullTextSession.createQuery( "from " + Restaurant.class.getName() ).list();
for (Object entity : events) {
fullTextSession.delete( entity );
}
tx.commit();
fullTextSession.close();
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
POI.class,
Event.class,
Hotel.class,
RangeHotel.class,
RangeEvent.class
RangeEvent.class,
Restaurant.class
};
}
}

0 comments on commit 963e51a

Please sign in to comment.