我有一个具有多对多关系的域对象。 Menu
可以包含多个Categories
,而Category
可以有多个Menus
。 Menu
负责Categories
。
我为我的应用程序做了相当分层的设计,域类与DAO
- s(我有一个特殊的同步类,用于访问Factory和Repository类并保持关系同步)和Application层(I在那里使用服务,例如所有逻辑所在的AddMenuService
。
例如,我创建了一个Menu
和Category
个对象。通过特殊的appender类将此类别添加到菜单中。然后我同步对象(验证,检查对象是否存在于数据库中或创建新对象,如果需要调用其他同步类,比较差异中的对象并保存或更新它们)。只能通过MenuSyncro添加类别。
Menu m1 = new Menu("Menu for vegetarians");
Category cat1 = new Category("Drinks");
m1.getAppender().addCategory(cat1);
MenuSyncro ms = new MenuSyncro();
ms.synchronize(menu1);
我知道我可以使用Dao.getEmptyForeignCollection(String fieldName);
,但有没有办法初始化ForeginCollection<MenuCategory> categories;
类中的Menu
和ForeignCollection<MenuCategory> menus;
类中的Category
而无法访问首先是DAO对象,因为我不断获得NullReference
例外,因为这些集合没有被初始化?
Menu.class
@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<MenuCategory> categories;
@ForeignCollectionField(eager = false)
private ForeignCollection<LocationMenu> 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<Category> getCategories()
{
ArrayList<Category> categoriesList = new ArrayList<Category>();
if (categories != null)
for (MenuCategory menuCategory : categories)
categoriesList.add(menuCategory.getCategory());
return categoriesList;
}
public ForeignCollection<LocationMenu> getLocations()
{
return locations;
}
public MenuAppender getAppender()
{
if (appender== null)
appender= new MenuAppender(this);
return appender;
}
}
Category.class
@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<MenuCategory> 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<Menu> getMenus()
{
ArrayList<Menu> menusList = new ArrayList<Menu>();
if (menus != null)
for (MenuCategory menuCategory : menus)
menusList.add(menuCategory.getMenu());
return menusList;
}
}
MenuCategory.class
@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;
}
}
先谢谢,恶人
答案 0 :(得分:-3)
只需拨打<lazyCollection>.iterator()
即可正确初始化您的收藏。