本文整理了Java中com.j256.ormlite.dao.Dao.assignEmptyForeignCollection()
方法的一些代码示例,展示了Dao.assignEmptyForeignCollection()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Dao.assignEmptyForeignCollection()
方法的具体详情如下:
包路径:com.j256.ormlite.dao.Dao
类名称:Dao
方法名:assignEmptyForeignCollection
[英]Creates an empty collection and assigns it to the appropriate field in the parent object. This allows you to add things to the collection from the start.
For example let's say you have an Account which has the field:
@ForeignCollectionField(columnName = "orders")
Collection<Order> orders;
You would then call:
accoundDao.assignEmptyForeignCollection(account, "orders");
// this would add it the collection and the internal DAO
account.orders.add(order1);
[中]创建空集合并将其分配给父对象中的相应字段。这允许您从一开始就将内容添加到集合中。
例如,假设您有一个帐户,其中包含以下字段:
@ForeignCollectionField(columnName = "orders")
Collection<Order> orders;
然后你会打电话:
accoundDao.assignEmptyForeignCollection(account, "orders");
// this would add it the collection and the internal DAO
account.orders.add(order1);
代码示例来源:origin: j256/ormlite-core
/**
* @see Dao#assignEmptyForeignCollection(Object, String)
*/
@Override
public void assignEmptyForeignCollection(T parent, String fieldName) {
try {
dao.assignEmptyForeignCollection(parent, fieldName);
} catch (SQLException e) {
logMessage(e, "assignEmptyForeignCollection threw exception on " + fieldName);
throw new RuntimeException(e);
}
}
代码示例来源:origin: com.j256.ormlite/ormlite-core
/**
* @see Dao#assignEmptyForeignCollection(Object, String)
*/
@Override
public void assignEmptyForeignCollection(T parent, String fieldName) {
try {
dao.assignEmptyForeignCollection(parent, fieldName);
} catch (SQLException e) {
logMessage(e, "assignEmptyForeignCollection threw exception on " + fieldName);
throw new RuntimeException(e);
}
}
代码示例来源:origin: Raizlabs/AndroidDatabaseLibraryComparison
/**
* We have to do this separate step because OrmLite doesn't automatically insert children
*/
public void insertNewContacts(Dao<AddressBook, Integer> addressBookDao, Dao<Contact, Integer> contactDao) throws SQLException {
if (this.nonDaoContacts == null) {
return;
}
addressBookDao.assignEmptyForeignCollection(this, "contacts");
for (Contact contact : nonDaoContacts) {
contact.setAddressBook(this);
contactDao.create(contact);
contacts.add(contact);
}
}
代码示例来源:origin: Raizlabs/AndroidDatabaseLibraryComparison
/**
* We have to do this separate step because OrmLite doesn't automatically insert children
*/
public void insertNewAddresses(Dao<AddressBook, Integer> addressBookDao, Dao<AddressItem, Integer> addressItemDao) throws SQLException {
if (this.nonDaoAddresses == null) {
return;
}
addressBookDao.assignEmptyForeignCollection(this, "addresses");
for (AddressItem addressItem : nonDaoAddresses) {
addressItem.setAddressBook(this);
addressItemDao.create(addressItem);
addresses.add(addressItem);
}
}
代码示例来源:origin: j256/ormlite-core
@Test
public void testAssignEmptyForeignCollection() throws Exception {
@SuppressWarnings("unchecked")
Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
dao.assignEmptyForeignCollection(null, null);
replay(dao);
rtDao.assignEmptyForeignCollection(null, null);
verify(dao);
}
代码示例来源:origin: j256/ormlite-core
@Test(expected = RuntimeException.class)
public void testAssignEmptyForeignCollectionThrow() throws Exception {
@SuppressWarnings("unchecked")
Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
dao.assignEmptyForeignCollection(null, null);
expectLastCall().andThrow(new SQLException("Testing catch"));
replay(dao);
rtDao.assignEmptyForeignCollection(null, null);
verify(dao);
}
代码示例来源:origin: j256/ormlite-core
@Test
public void testAddNoForeign() throws Exception {
createTable(Order.class, true);
Dao<Account, Integer> accountDao = createDao(Account.class, true);
Account account = new Account();
String name = "fwepfjewfew";
account.name = name;
assertEquals(1, accountDao.create(account));
accountDao.assignEmptyForeignCollection(account, Account.ORDERS_FIELD_NAME);
Order order1 = new Order();
order1.val = 1453783141;
account.orders.add(order1);
Order order2 = new Order();
order2.val = 247895295;
account.orders.add(order2);
Account result = accountDao.queryForId(account.id);
assertNotNull(result);
Iterator<Order> iterator = result.orders.iterator();
assertTrue(iterator.hasNext());
assertEquals(order1.val, iterator.next().val);
assertTrue(iterator.hasNext());
assertEquals(order2.val, iterator.next().val);
assertFalse(iterator.hasNext());
}
代码示例来源:origin: j256/ormlite-core
@Test
public void testAddDontOverwriteForeign() throws Exception {
createTable(Order.class, true);
Dao<Account, Integer> accountDao = createDao(Account.class, true);
Account account1 = new Account();
account1.name = "fwepfjewfew";
assertEquals(1, accountDao.create(account1));
accountDao.assignEmptyForeignCollection(account1, Account.ORDERS_FIELD_NAME);
Account account2 = new Account();
account2.name = "fhwoifwepfjewfew";
assertEquals(1, accountDao.create(account2));
Order order1 = new Order();
order1.val = 1453783141;
order1.account = account2;
account1.orders.add(order1);
Order order2 = new Order();
order2.val = 247895295;
order2.account = account2;
account1.orders.add(order2);
Account result = accountDao.queryForId(account1.id);
assertNotNull(result);
// we don't find any because they were alreayd set with account2's id
Iterator<Order> iterator = result.orders.iterator();
assertFalse(iterator.hasNext());
}
内容来源于网络,如有侵权,请联系作者删除!