Note that there are some explanatory texts on larger screens.

plurals
  1. POForeignCollection initialization in domain class
    text
    copied!<p>I have a domain objects with many-to-many relationship. <code>Menu</code> can have many <code>Categories</code> and <code>Category</code> can be in many <code>Menus</code>. <code>Menu</code> is responsible for <code>Categories</code>.</p> <p>I have made quite layered design for my application and domain classes are strictly separated from <code>DAO</code>-s (I have a special synchronization classes for accessing Factory and Repository classes and keeping relationships in sync) and Application layer (I use services there, for example <code>AddMenuService</code> where all the logic resides). </p> <p>For example I create a <code>Menu</code> and <code>Category</code> objects. Add this category to menu via special appender class. Then I synchronize objects (validate, check if object exists in database or create a new object, calling other Synchro classes if needed, comparing objects from difference and saving or updating them). Categories can be added only through MenuSyncro.</p> <pre><code>Menu m1 = new Menu("Menu for vegetarians"); Category cat1 = new Category("Drinks"); m1.getAppender().addCategory(cat1); MenuSyncro ms = new MenuSyncro(); ms.synchronize(menu1); </code></pre> <p>I know that I can use <code>Dao.getEmptyForeignCollection(String fieldName);</code>, but is there any way to initialize <code>ForeginCollection&lt;MenuCategory&gt; categories;</code> in <code>Menu</code> class and <code>ForeignCollection&lt;MenuCategory&gt; menus;</code> in <code>Category</code> class without to access DAO objects first, because i keep getting <code>NullReference</code> exceptions because those collections are not initalized?</p> <h2>Classes</h2> <p><strong>Menu.class</strong></p> <pre><code>@DatabaseTable(tableName = "MENUS") public class Menu { private final static String ID_FIELD_NAME = "ID"; private final static String NAME_FIELD_NAME = "NAME"; @DatabaseField(generatedId = true, columnName = ID_FIELD_NAME) private int id; @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false, unique = true, useGetSet = true) private String name; @ForeignCollectionField(eager = false) private ForeignCollection&lt;MenuCategory&gt; categories; @ForeignCollectionField(eager = false) private ForeignCollection&lt;LocationMenu&gt; locations; private MenuAppender appender; public Menu() { // for ORMLite } public Menu(String name) { this.name = name; } public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList&lt;Category&gt; getCategories() { ArrayList&lt;Category&gt; categoriesList = new ArrayList&lt;Category&gt;(); if (categories != null) for (MenuCategory menuCategory : categories) categoriesList.add(menuCategory.getCategory()); return categoriesList; } public ForeignCollection&lt;LocationMenu&gt; getLocations() { return locations; } public MenuAppender getAppender() { if (appender== null) appender= new MenuAppender(this); return appender; } } </code></pre> <p><strong>Category.class</strong></p> <pre><code>@DatabaseTable(tableName = "CATEGORIES") public class Category { private final static String ID_FIELD_NAME = "ID"; private final static String NAME_FIELD_NAME = "NAME"; @DatabaseField(generatedId = true, columnName = ID_FIELD_NAME) private int id; @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false, unique = true, useGetSet = true) private String name; @ForeignCollectionField(eager = false) private ForeignCollection&lt;MenuCategory&gt; menus; public Category() { // for ORMLite } public Category(String name) { this.name = name; } public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList&lt;Menu&gt; getMenus() { ArrayList&lt;Menu&gt; menusList = new ArrayList&lt;Menu&gt;(); if (menus != null) for (MenuCategory menuCategory : menus) menusList.add(menuCategory.getMenu()); return menusList; } </code></pre> <p>}</p> <p><strong>MenuCategory.class</strong></p> <pre><code> @DatabaseTable(tableName = "MENUS_CATEGORIES") public class MenuCategory { private final static String ID_FIELD_NAME = "ID"; private final static String MENUS_ID_FIELD_NAME = "MENUS_ID"; private final static String CATEGORIES_ID_FIELD_NAME = "CATEGORIES_ID"; @SuppressWarnings("unused") @DatabaseField(generatedId = true, columnName = ID_FIELD_NAME) private int id; @DatabaseField(foreign = true, columnName = MENUS_ID_FIELD_NAME, canBeNull = false, foreignAutoCreate = true, foreignAutoRefresh = true) private Menu menu; @DatabaseField(foreign = true, columnName = CATEGORIES_ID_FIELD_NAME, canBeNull = false, foreignAutoCreate = true, foreignAutoRefresh = true) private Category category; public MenuCategory() { // for ORMLite } public MenuCategory(Menu menu, Category category) { this.menu = menu; this.category = category; } public Menu getMenu() { return menu; } public void setMenu(Menu menu) { this.menu = menu; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } } </code></pre> <p>Thanks in advance, evilone</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload