2013-02-19 4 views
2

나는 내 모든 프로젝트를 삭제, 생성 및 나열하는 몇 가지 방법이있는 "Journey"모델 클래스가 있습니다. 나는 heroku와 postgresql 데이터베이스를 사용하고 있습니다. 지정된 주소와 비슷한 주소를 가진 모든 여행을 반환하는 메서드를 작성해야합니다. 쿼리 구조는 일반적으로 SELECT address FROM Journey WHERE address ~~ arguement과 같을 것입니다. 그러나이 기능을 플레이 프레임 워크에서 수행하는 것이 어떤 기능인지는 알지 못합니다. 당신이 당신의 모델이를 추가 할 수 있습니다 기본 경우플레이 프레임 워크 데이터베이스 검색

List<Journey> allJourneys = Journey.find.all(); 
List<Journey> searchedJourneys = Journey.find.where().like("address", "%foo%").findList(); 
Journey firstJourney = Journey.find.byId(123); 

:

package models; 

import play.db.ebean.Model; 

import javax.persistence.*; 

@Entity 
public class Journey extends Model { 

    @Id 
    public Integer id; 

    public static Finder<Integer, Journey> find 
      = new Model.Finder<>(Integer.class, Journey.class); 

    // other fields 
    public String address; 
    public String country; 

} 

그렇게 쉽게 레코드를 선택할 수 있습니다

*public static void search(String address){ 
//query 
//return matching journey results 
}* 
+0

ebean을 사용하고 있습니까? – biesior

+0

예. application.conf - ebean.default = "models. *" –

답변

5

당신은 예를 들어 모델의 Finder를 사용할 필요가 :

public static List<Journey> searchByAddress(String address){ 
    return find.where().like("address", "%"+address+"%").findList(); 
} 

등 관계를 가진 전체 객체를 반환하므로 큰 데이터 세트에서는 너무 무거울 수 있습니다. select(), fetch() 등과 같은 Finder의 체인 방식으로 더 최적화 된 쿼리를 사용하여 현재 필요한 데이터를 가리킬 수도 있고 사용할 수도 있습니다.

Ebean's API에 다른 가능성이 있습니다. 어쨌든 어떤 접근 방식이 가장 적합할지 선언해야합니다.

현재 ORW에 익숙해지기 위해 computer's database 예를 들어 기존 샘플 응용 프로그램을 검토해 볼 가치가 있습니다. 검색 insesitive 경우에 대해

편집

ilike (like 대신의) 추가 식 istartsWith, iendsWith, ieq, icontainsiexampleLike있다. 처음에는 i이없는 버전과 동일합니다.

in the API도 미리 볼 수 있습니다.

+0

에 대한 답변이 늦었습니다. 이것은 위대합니다. 많은 것을 만들어 주셔서 감사합니다. 이 "좋아하는"선택자로 대소 문자를 무시하는 방법이 있습니까? –

+0

마지막으로 대소 문자를 구분하지 않는 식을 확인하십시오. – biesior