io.objectbox.Box.get()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(168)

本文整理了Java中io.objectbox.Box.get()方法的一些代码示例,展示了Box.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Box.get()方法的具体详情如下:
包路径:io.objectbox.Box
类名称:Box
方法名:get

Box.get介绍

[英]Get the stored object for the given ID.
[中]

代码示例

代码示例来源:origin: objectbox/objectbox-java

/** If property backed, entities can pass the target ID to avoid reflection. */
@Internal
public TARGET getTarget(long targetId) {
  synchronized (this) {
    if (resolvedTargetId == targetId) {
      return target;
    }
  }
  ensureBoxes(null);
  // Do not synchronize while doing DB stuff
  TARGET targetNew = targetBox.get(targetId);
  setResolvedTarget(targetNew, targetId);
  return targetNew;
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testGetIds() {
  List<TestEntity> entities = putTestEntities(5);
  List<Long> ids = new ArrayList<>();
  ids.add(entities.get(1).getId());
  ids.add(entities.get(3).getId());
  List<TestEntity> readEntities = box.get(ids);
  assertEquals(2, readEntities.size());
  assertEquals((long) ids.get(0), readEntities.get(0).getId());
  assertEquals((long) ids.get(1), readEntities.get(1).getId());
  Map<Long, TestEntity> map = box.getMap(ids);
  assertEquals(2, map.size());
  assertEquals((long) ids.get(0), map.get(ids.get(0)).getId());
  assertEquals((long) ids.get(1), map.get(ids.get(1)).getId());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testGetAllAfterGetAndRemove() {
  assertEquals(0, box.count());
  assertEquals(0, box.getAll().size());
  System.out.println("PUT");
  List<TestEntity> entities = putTestEntities(10);
  // explicitly get an entity (any will do)
  System.out.println("GET");
  TestEntity entity = box.get(entities.get(1).getId());
  assertNotNull(entity);
  System.out.println("REMOVE_ALL");
  box.removeAll();
  System.out.println("COUNT");
  assertEquals(0, box.count());
  System.out.println("GET_ALL");
  List<TestEntity> all = box.getAll();
  // note only 1 entity is returned by getAll, it is the one we explicitly get (last) above
  assertEquals(0, all.size());
}

代码示例来源:origin: objectbox/objectbox-java

@Override
  public void run() {
    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    EntityLongIndex unique = query.findUnique();
    assertNull(unique);
    latchRead1.countDown();
    System.out.println("BEFORE put: " + box.getReaderDebugInfo());
    System.out.println("count before: " + box.count());
    try {
      latchPut.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
    System.out.println("AFTER put: " + box.getReaderDebugInfo());
    System.out.println("count after: " + box.count());
    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    results[0] = query.findUnique();
    results[1] = box.get(1);
    results[2] = query.findUnique();
    query.setParameter(EntityLongIndex_.indexedLong, 0);
    results[3] = query.findUnique();
    latchRead2.countDown();
  }
}.start();

代码示例来源:origin: objectbox/objectbox-java

@Override
  public Long call() throws Exception {
    assertNotSame(mainTestThread, Thread.currentThread());
    for (int i = 0; i < countEntities; i++) {
      TestEntity entity = new TestEntity();
      final int value = number.incrementAndGet();
      entity.setSimpleInt(value);
      long key = box.put(entity);
      TestEntity read = box.get(key);
      assertEquals(value, read.getSimpleInt());
    }
    return box.count();
  }
};

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testGetInTx() {
  Customer customer = putCustomerWithOrders(2);
  customer = customerBox.get(customer.getId());
  final ToMany<Order> toMany = customer.getOrdersStandalone();
  store.runInReadTx(new Runnable() {
    @Override
    public void run() {
      assertGetOrder1And2(toMany);
    }
  });
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testGet() {
  Customer customer = putCustomerWithOrders(2);
  customer = customerBox.get(customer.getId());
  final ToMany<Order> toMany = customer.getOrdersStandalone();
  //        RelationInfo<Order> info = Customer_.ordersStandalone;
  //        int sourceEntityId = info.sourceInfo.getEntityId();
  //        assertEquals(2, orderBox.internalGetRelationEntities(sourceEntityId, info.relationId, customer.getId()).size());
  assertGetOrder1And2(toMany);
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testToManyBulk() {
  // JNI local refs are limited on Android (for example, 512 on Android 7)
  final int count = runExtensiveTests ? 10000 : 1000;
  Customer customer = new Customer();
  List<Order> orders = customer.getOrders();
  List<Order> ordersStandalone = customer.getOrdersStandalone();
  for (int i = 0; i < count; i++) {
    Order order = new Order();
    order.setText("order" + i);
    orders.add(order);
    Order orderStandalone = new Order();
    orderStandalone.setText("orderStandalone" + i);
    ordersStandalone.add(orderStandalone);
  }
  long customerId = customerBox.put(customer);
  assertEquals(count, customerBox.get(customerId).getOrders().size());
  assertEquals(count, customerBox.get(customerId).getOrdersStandalone().size());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testBasics() {
  ObfuscatedEntity object = new ObfuscatedEntity();
  long id = box.put(object);
  assertTrue(id > 0);
  assertEquals(id, (long) object.getId());
  ObfuscatedEntity objectRead = box.get(id);
  assertNotNull(objectRead);
  assertEquals(id, (long) objectRead.getId());
  assertEquals(1, box.count());
  box.remove(id);
  assertEquals(0, box.count());
  assertNull(box.get(id));
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testPutAndGetTwoEntities() {
  store.close();
  store.deleteAllFiles();
  store = createBoxStoreBuilderWithTwoEntities(false).build();
  box = store.boxFor(TestEntity.class);
  long key = putTestEntity(null, 1977).getId();
  TestEntity entityRead = box.get(key);
  assertEquals(1977, entityRead.getSimpleInt());
  Box<TestEntityMinimal> box2 = store.boxFor(TestEntityMinimal.class);
  TestEntityMinimal entity2 = new TestEntityMinimal();
  entity2.setText("foo");
  long key2 = box2.put(entity2);
  TestEntityMinimal entity2Read = box2.get(key2);
  assertEquals("foo", entity2Read.getText());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testPutAndGet() {
  TestEntity entity = new TestEntity();
  entity.setSimpleInt(1977);
  long key = box.put(entity);
  assertTrue(key != 0);
  assertEquals(key, entity.getId());
  TestEntity entityRead = box.get(key);
  assertNotNull(entityRead);
  assertEquals(1977, entityRead.getSimpleInt());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testClear() {
  int count = 5;
  Customer customer = putCustomerWithOrders(count);
  ToMany<Order> toMany = customer.ordersStandalone;
  toMany.clear();
  customerBox.put(customer);
  Customer customer2 = customerBox.get(customer.getId());
  assertEquals(0, customer2.getOrdersStandalone().size());
  assertEquals(count, orderBox.count());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testAttach() {
  Customer customer = putCustomer();
  putOrder(customer, "Bananas");
  putOrder(customer, "Oranges");
  Customer customerNew = new Customer();
  customerNew.setId(customer.getId());
  customerBox.attach(customerNew);
  assertEquals(2, customerNew.getOrders().size());
  customerNew.setName("Jake");
  customerBox.put(customerNew);
  assertEquals("Jake", customerBox.get(customer.getId()).getName());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testReset() {
  Customer customer = putCustomerWithOrders(2);
  customer = customerBox.get(customer.getId());
  ToMany<Order> toMany = customer.getOrdersStandalone();
  assertEquals(2, toMany.size());
  Customer customer2 = customerBox.get(customer.getId());
  customer2.getOrdersStandalone().add(putOrder(null, "order3"));
  customer2.getOrdersStandalone().applyChangesToDb();
  assertEquals(2, toMany.size());
  toMany.reset();
  assertFalse(toMany.isResolved());
  assertEquals(3, toMany.size());
  assertTrue(toMany.isResolved());
}

代码示例来源:origin: objectbox/objectbox-java

private void testPutCustomerWithOrders(Customer customer, int countNewOrders, int countExistingOrders) {
  for (int i = 1; i <= countNewOrders; i++) {
    Order order = new Order();
    order.setText("new" + i);
    customer.orders.add(order);
  }
  for (int i = 1; i <= countExistingOrders; i++) {
    customer.orders.add(putOrder(null, "existing" + i));
  }
  ToMany<Order> toMany = (ToMany<Order>) customer.orders;
  assertEquals(countNewOrders + countExistingOrders, toMany.getAddCount());
  customerBox.put(customer);
  assertEquals(1, customer.getId());
  assertEquals(0, toMany.getAddCount());
  for (int i = 1; i <= countNewOrders; i++) {
    assertEquals(countExistingOrders + i, customer.orders.get(i - 1).getId());
  }
  assertEquals(1, customerBox.count());
  assertEquals(countNewOrders + countExistingOrders, orderBox.count());
  for (Order order : customer.orders) {
    assertEquals(customer.getId(), order.getCustomerId());
    assertEquals(customer.getId(), orderBox.get(order.getId()).getCustomerId());
  }
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testRelationToOne() {
  Customer customer = putCustomer();
  Order order = putOrder(customer, "Bananas");
  Order order1 = orderBox.get(order.getId());
  assertEquals(customer.getId(), order1.getCustomerId());
  assertNull(order1.peekCustomer());
  assertEquals(customer.getId(), order1.getCustomer().getId());
  assertNotNull(order1.peekCustomer());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
@Ignore("not yet supported")
public void testPutSourceAndNewTarget() {
  Order source = new Order();
  source.setText("source");
  orderBox.put(source);
  Customer target = new Customer();
  target.setName("target1");
  ToOne<Customer> toOne = new ToOne<>(source, getRelationInfo(Order_.customerId));
  toOne.setTarget(target);
  assertTrue(target.getId() != 0);
  assertEquals("target1", customerBox.get(target.getId()).getName());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testGet() {
  Customer customer = putCustomerWithOrders(2);
  customer = customerBox.get(customer.getId());
  ToMany<Order> toMany = (ToMany<Order>) customer.getOrders();
  assertFalse(toMany.isResolved());
  assertEquals(2, toMany.size());
  assertTrue(toMany.isResolved());
  assertEquals("order1", toMany.get(0).getText());
  assertEquals("order2", toMany.get(1).getText());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testReset() {
  Customer customer = putCustomerWithOrders(2);
  customer = customerBox.get(customer.getId());
  ToMany<Order> toMany = (ToMany<Order>) customer.getOrders();
  assertEquals(2, toMany.size());
  putOrder(customer, "order3");
  assertEquals(2, toMany.size());
  toMany.reset();
  assertFalse(toMany.isResolved());
  assertEquals(3, toMany.size());
  assertTrue(toMany.isResolved());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testSyncToTargetBox() {
  int count = 5;
  Customer customer = putCustomerWithOrders(count);
  ToMany<Order> toMany = (ToMany<Order>) customer.orders;
  Order order = toMany.get(2);
  assertTrue(toMany.retainAll(Collections.singletonList(order)));
  toMany.add(putOrder(null, "new1"));
  Order order2 = new Order();
  order2.setText("new2");
  toMany.add(order2);
  assertEquals(4, toMany.getRemoveCount());
  assertEquals(2, toMany.getAddCount());
  toMany.applyChangesToDb();
  assertEquals(0, toMany.getRemoveCount());
  assertEquals(0, toMany.getAddCount());
  assertEquals(count + 2, orderBox.count());
  toMany.reset();
  assertEquals(3, toMany.size());
  assertEquals("order3", toMany.get(0).getText());
  assertEquals("new1", toMany.get(1).getText());
  assertEquals("new2", toMany.get(2).getText());
  assertFalse(toMany.internalCheckApplyToDbRequired());
  customerBox.put(customer);
  assertEquals(3, customerBox.get(customer.getId()).orders.size());
}

相关文章