2013-05-15 3 views
2

Robospice/ORMlite를 통해 중첩 된 (2-3 레벨 중첩) 객체 (외부 필드)를 캐싱 할 수 있습니까? https://groups.google.com/forum/#!msg/robospice/VGLB3-vM3Ug/-piOac212HYJ - 거기서 읽을 수 있지만 불행히도 나는 그것을 성취 할 수 없습니다.Robispice에서 ORMLite로 중첩 된 객체 캐싱

@DatabaseTable(tableName = "city") 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class City { 
    @DatabaseField(id = true) 
    @JsonProperty("id") 
    private long id; 
    @DatabaseField 
    @JsonProperty("name") 
    private String name; 
    @ForeignCollectionField(eager = true, maxEagerLevel = 3) 
    @JsonProperty("clubs") 
    private Collection<Club> clubs; 
    ... 

@DatabaseTable(tableName = "club") 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class Club { 
    @DatabaseField(id = true) 
    @JsonProperty("user_id") 
    private long id; 
    @DatabaseField 
    @JsonProperty("name") 
    private String name; 
    @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "city_id", maxForeignAutoRefreshLevel = 2) 
    private City city; 
    @DatabaseField(foreign = true) 
    @JsonProperty("address") 
    private VenueAddress address; 
... 

@DatabaseTable(tableName = "address") 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class VenueAddress { 
    @DatabaseField(id = true) 
    @JsonProperty("uid") 
    private long id; 
    @DatabaseField 
    @JsonProperty("street") 
    private String street; 
    @DatabaseField 
    @JsonProperty("street_number") 
    private String streetNumber; 
    @DatabaseField 
    @JsonProperty("country") 
    private String country; 
    @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "club_id", maxForeignAutoRefreshLevel = 2) 
    private Club club; 
... 

그리고 샘플 SpiceService : 여기

내 소스 코드

public class SampleSpiceService extends SpringAndroidSpiceService { 

    private static final int WEBSERVICES_TIMEOUT = 10000; 

    @Override 
    public CacheManager createCacheManager(Application application) { 
     CacheManager cacheManager = new CacheManager(); 
     List<Class<?>> classCollection = new ArrayList<Class<?>>(); 

     // add persisted classes to class collection 
     classCollection.add(VenueAddress.class); 
     classCollection.add(City.class); 
     classCollection.add(Club.class); 
     // init 
     RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper(application, 
       "sample_database.db", 5); 
     InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory(
       application, databaseHelper, classCollection); 
     cacheManager.addPersister(inDatabaseObjectPersisterFactory); 
     return cacheManager; 
    } 

    @Override 
    public RestTemplate createRestTemplate() { 
     RestTemplate restTemplate = new RestTemplate(); 
     // set timeout for requests 

     HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); 
     httpRequestFactory.setReadTimeout(WEBSERVICES_TIMEOUT); 
     httpRequestFactory.setConnectTimeout(WEBSERVICES_TIMEOUT); 
     restTemplate.setRequestFactory(httpRequestFactory); 

     MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter(); 
     restTemplate.getMessageConverters().add(messageConverter); 
     restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); 

     return restTemplate; 
    } 

} 

내가 캐시에서 도시 객체를 가져올 때 (이것은 클럽 수집을 가지고 있지만 각 클럽에 VenueAddress가 null 필드가 id 제외). 조언이 있으십니까?

답변

1

이것은 RoboSpice보다 더 많은 ORM Lite 질문입니다.

어쩌면 클럽 소유권/테이블에 주소 데이터를 "포함시킬"수 있습니다.

이 스레드는 관심의 대상이 될 수 있습니다 : 이 one-to-one relationship in Ormlite

ormlite docs에 명시된 바와 같이 문제는 여기에서 올 수

:

foreignAutoRefresh

를이 사실로 설정 (디폴트는 false)를 가지고 객체가 쿼리 될 때 외부 필드가 자동으로 새로 고쳐집니다. 기본값은 검색된 개체의 ID 필드를 가지고 호출자가 올바른 DAO에서 새로 고침을 호출하는 것입니다. 이 값을 true로 설정하면 개체를 쿼리 할 때 내부 DAO를 통해 외부 개체의 필드를로드하는 별도의 데이터베이스 호출이 수행됩니다. 참고 :이 필드가 설정된 개체를 만드는 경우이 자동으로 이물질을 만들지 않습니다.

참고 : 이렇게하면 내부적으로 다른 DAO 개체가 만들어지기 때문에 메모리가 부족하면 장치를 손으로 새로 고침 할 수 있습니다.

참고 : 재귀로부터 보호하려면 자동 새로 고침이 제한되어있는 곳이 두 곳 있습니다. foreignAutoRefresh가 true로 설정된 필드가있는 클래스를 자동 새로 고치거나 외부 컬렉션이있는 클래스를 자동 새로 고치면 두 경우 모두 결과 필드가 null로 설정되고 자동 새로 고침되지 않습니다. 필요한 경우 언제든지 필드에서 새로 고침을 직접 호출 할 수 있습니다.

+0

답변을 구하십시오. 예를 들어 객체를 포함하는 것이 좋습니다. 하지만 더 깊숙이 중첩 된 클래스가 더 있습니다. 그러나 결국에는 RoboSpice에서 요청을 캐시하는 다른 방법을 선택했습니다 (기본값, JacksonSpringAndroidSpiceService에서 제공됨). 필자의 경우 성능에는 차이가 없다. –