使用ORMLite在Robospice中缓存嵌套对象

时间:2013-05-15 13:49:11

标签: android ormlite robospice

是否可以通过Robospice / ORMlite缓存嵌套(2-3级嵌套)对象(外部字段)? https://groups.google.com/forum/#!msg/robospice/VGLB3-vM3Ug/-piOac212HYJ - 在那里你可以读到这是可能的,但不幸的是我无法实现它。

这是我的源代码:

@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;
    }

}

当我从缓存中获取City对象时,它有club集合,但每个Club中的VenueAddress都有空字段(id除外)。 你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是一个ORM Lite问题,而不是RoboSpice的问题。

也许你可以将地址数据“嵌入”在俱乐部权利/表格中。

此主题可能会引起关注: one-to-one relationship in Ormlite

问题可能来自ormlite docs

中所述
  

foreignAutoRefresh

     

将此项设置为true(默认为false),以便在查询对象时自动刷新外部字段。默认情况下,只检索对象中的ID字段,并让调用者在正确的DAO上调用refresh。如果将其设置为true,则在查询对象时,将通过内部DAO单独调用数据库以加载外部对象的字段。注意:如果您创建一个具有此字段集的对象,则不会自动创建外部对象。

     

注意:这将在内部创建另一个DAO对象,因此低内存设备可能需要手动调用刷新。

     

注意:为防止递归,有几个地方被自动刷新受到限制。如果要自动刷新一个本身具有foreignAutoRefresh设置为true的字段的类,或者如果要自动刷新具有外部集合的类,则在这两种情况下,结果字段将设置为null并且不会自动刷新。如果需要,您可以直接在该字段上调用刷新。