Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseTableConfig for foreign collection #38

Closed
martinbrylski opened this issue Jan 14, 2015 · 2 comments
Closed

DatabaseTableConfig for foreign collection #38

martinbrylski opened this issue Jan 14, 2015 · 2 comments

Comments

@martinbrylski
Copy link

Hi,
I want to create DatabaseTableConfigs for two classes, with a one-to-many relationship:

Take this as example:

class A {
   String uuid;
   Collection<B> bs;
}

class B {
   String uuid;
   A a;
}

I get No fields have a DatabaseField annotation in class B with my current setup and I can not find any help regarding this problem.

I have something like this as my current config:

....
TableUtils.createTable(connectionSource, getATableConfig());
TableUtils.createTable(connectionSource, getBTableConfig());

....

DatabaseTableConfig<A> getATableConfig() {
    List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();

    DatabaseFieldConfig fieldUuid = new DatabaseFieldConfig("uuid");
    fieldUuid.setCanBeNull(false);
    fieldUuid.setId(true);
    fieldConfigs.add(fieldUuid);

   DatabaseFieldConfig fieldBs = new DatabaseFieldConfig("bs");
   fieldBs.setCanBeNull(true);
   fieldBs.setForeignCollection(true);
   fieldBs.setForeignCollectionEager(true);
   fieldBs.setForeignCollectionForeignFieldName("a");
   fieldConfigs.add(fieldBs);

   return new DatabaseTableConfig<>(A.class, fieldConfigs);
}

DatabaseTableConfig<B> getBTableConfig() {
    List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();

    DatabaseFieldConfig fieldUuid = new DatabaseFieldConfig("uuid");
    fieldUuid.setCanBeNull(false);
    fieldUuid.setId(true);
    fieldConfigs.add(fieldUuid);

    DatabaseFieldConfig fieldA = new DatabaseFieldConfig("a");
    fieldA.setCanBeNull(true);
    fieldA.setForeign(true);
    fieldA.setForeignAutoRefresh(true);
    fieldA.setForeignTableConfig(getATableConfig());
    fieldConfigs.add(fieldA);

    return new DatabaseTableConfig<>(B.class, fieldConfigs);
}
@martinbrylski
Copy link
Author

It seems there is a circular dependency creating the DatabaseFieldConfig for the foreign collection and the foreign field. They both want to know each others table config. Using a "fake" third table config with less properties it will work.

@j256
Copy link
Owner

j256 commented Feb 16, 2015

There is a circular dependency but all you have to do is configure the b table config first, without setting the fieldA.setForeignTableConfig(...), then do the a table config and save it in a variable, then go back and call fieldA.setForeignTableConfig(fieldATableConfig).

@j256 j256 closed this as completed Feb 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants