본문 바로가기

Development/Web & Server

[Spring Framework] Hibernate 4 Search 사용하기~! (Latitude, Longitude 검색)

Hibernate 4를 연동했다는 전제에 설명을 하겠습니다.~!


1. pom.xml파일


<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-search</artifactId>

<version>4.4.2.Final</version>

</dependency>


2. 모델 설정하기~!


@Entity

@Spatial

@Indexed

@Table (name = "db tableName~!")

public class SomeThing {

public int getmId() {

return mId;

}

public void setmId(int mId) {

this.mId = mId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Double getLastLongitude() {

return lastLongitude;

}

public void setLastLongitude(Double lastLongitude) {

this.lastLongitude = lastLongitude;

}

public Double getLastLatitude() {

return lastLatitude;

}

public void setLastLatitude(Double lastLatitude) {

this.lastLatitude = lastLatitude;

}


@Id

@Column (name = "id")

@GeneratedValue

private Integer mId;

@Column (name = "name")

private String name;

@Column (name = "last_latitude")

@Latitude

private Double lastLatitude;

@Column (name = "last_longitude")

@Longitude

private Double lastLongitude;

}


3. 사용하기


List<SomeThing> resultList = null;


try {

final FullTextSession fullTextSession = Search.getFullTextSession(this.mSessionFactory.getCurrentSession());

fullTextSession.createIndexer().startAndWait();

final QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(SomeThing.class).get();

final org.apache.lucene.search.Query searchQuery = builder.spatial()

.onDefaultCoordinates()

  .within(range, Unit.KM)

  .ofLatitude(latitude)

          .andLongitude(longitude)

  .createQuery();

final FullTextQuery hibQuery = fullTextSession.createFullTextQuery(searchQuery, SomeThing.class);

resultList = hibQuery.list();


} catch (InterruptedException e) {

e.printStackTrace();

}