io.objectbox.query.Query.findUnique()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(219)

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

Query.findUnique介绍

[英]Find the unique Object matching the query.
[中]查找与查询匹配的唯一对象。

代码示例

代码示例来源: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

@Test
public void testSetParameterString() {
  putTestEntitiesStrings();
  Query<TestEntity> query = box.query().equal(simpleString, "banana").parameterAlias("foo").build();
  assertEquals(1, query.findUnique().getId());
  query.setParameter(simpleString, "bar");
  assertEquals(3, query.findUnique().getId());
  assertNull(query.setParameter(simpleString, "not here!").findUnique());
  query.setParameter("foo", "apple");
  assertEquals(2, query.findUnique().getId());
}

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

@Test
public void testOldReaderWithIndex() throws InterruptedException {
  final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class);
  final int initialValue = 1;
  final Query<EntityLongIndex> query = box.query().equal(EntityLongIndex_.indexedLong, 0).build();
  assertNull(query.findUnique());
  System.out.println("BEFORE put: " + box.getReaderDebugInfo());
  System.out.println("count before: " + box.count());
  box.put(createEntityLongIndex(initialValue));
  System.out.println("AFTER put: " + box.getReaderDebugInfo());
  System.out.println("count after: " + box.count());
  query.setParameter(EntityLongIndex_.indexedLong, initialValue);
  assertNotNull(query.findUnique());
  query.setParameter(EntityLongIndex_.indexedLong, 0);
  assertNull(query.findUnique());
}

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

@Test
public void testSetParameterInt() {
  String versionNative = BoxStore.getVersionNative();
  String minVersion = "1.5.1-2018-06-21";
  String versionStart = versionNative.substring(0, minVersion.length());
  assertTrue(versionStart, versionStart.compareTo(minVersion) >= 0);
  putTestEntitiesScalars();
  Query<TestEntity> query = box.query().equal(simpleInt, 2007).parameterAlias("foo").build();
  assertEquals(8, query.findUnique().getId());
  query.setParameter(simpleInt, 2004);
  assertEquals(5, query.findUnique().getId());
  query.setParameter("foo", 2002);
  assertEquals(3, query.findUnique().getId());
}

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

assertNotNull(query.findUnique());
assertNotNull(query.findUnique());
assertNotNull(query.findUnique());

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

@Test
public void testSetParameter2Floats() {
  putTestEntitiesScalars();
  Query<TestEntity> query = box.query().between(simpleFloat, 400.15, 400.75).parameterAlias("foo").build();
  assertEquals(6, query.count());
  query.setParameters(simpleFloat, 400.65, 400.85);
  List<TestEntity> entities = query.find();
  assertEquals(2, entities.size());
  assertEquals(8, entities.get(0).getId());
  assertEquals(9, entities.get(1).getId());
  query.setParameters("foo", 400.45, 400.55);
  assertEquals(6, query.findUnique().getId());
}

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

@Test
public void testScalarEqual() {
  putTestEntitiesScalars();
  Query<TestEntity> query = box.query().equal(simpleInt, 2007).build();
  assertEquals(1, query.count());
  assertEquals(8, query.findFirst().getId());
  assertEquals(8, query.findUnique().getId());
  List<TestEntity> all = query.find();
  assertEquals(1, all.size());
  assertEquals(8, all.get(0).getId());
}

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

@Test
public void testSetParameter2Ints() {
  putTestEntitiesScalars();
  Query<TestEntity> query = box.query().between(simpleInt, 2005, 2008).parameterAlias("foo").build();
  assertEquals(4, query.count());
  query.setParameters(simpleInt, 2002, 2003);
  List<TestEntity> entities = query.find();
  assertEquals(2, entities.size());
  assertEquals(3, entities.get(0).getId());
  assertEquals(4, entities.get(1).getId());
  query.setParameters("foo", 2007, 2007);
  assertEquals(8, query.findUnique().getId());
}

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

@Test
public void testFailedUnique_exceptionListener() {
  final Exception[] exs = {null};
  DbExceptionListener exceptionListener = new DbExceptionListener() {
    @Override
    public void onDbException(Exception e) {
      exs[0] = e;
    }
  };
  putTestEntitiesStrings();
  Query<TestEntity> query = box.query().build();
  store.setDbExceptionListener(exceptionListener);
  try {
    query.findUnique();
    fail("Should have thrown");
  } catch (NonUniqueResultException e) {
    assertSame(e, exs[0]);
  }
}

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

assertNotNull(query.findUnique());
assertNotNull(query.findUnique());
assertNotNull(query.findUnique());

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

@Test
public void testString() {
  List<TestEntity> entities = putTestEntitiesStrings();
  int count = entities.size();
  assertEquals(1, box.query().equal(simpleString, "banana").build().findUnique().getId());
  assertEquals(count - 1, box.query().notEqual(simpleString, "banana").build().count());
  assertEquals(4, box.query().startsWith(simpleString, "ba").endsWith(simpleString, "shake").build().findUnique()
      .getId());
  assertEquals(2, box.query().contains(simpleString, "nana").build().count());
}

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

@Test
public void testRelationToOneQuery() {
  Customer customer = putCustomer();
  Order order = putOrder(customer, "Bananas");
  Query<Order> query = orderBox.query().equal(Order_.customerId, customer.getId()).build();
  Order orderFound = query.findUnique();
  assertEquals(order.getId(), orderFound.getId());
}

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

order = orderBox.query().eager(Order_.customer).build().findUnique();
assertTrue(order.customer__toOne.isResolved());

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

customer = customerBox.query().eager(Customer_.orders).build().findUnique();
assertTrue(((ToMany) customer.getOrders()).isResolved());

代码示例来源:origin: owntracks/android

@Override
public org.owntracks.android.data.WaypointModel get(long tst) {
  return this.box.query().equal(WaypointModel_.tst,tst).build().findUnique();
}

代码示例来源:origin: fengzhizi715/RxCache4a

@Override
public void evict(String key) {
  CacheEntity entity = cacheEntityBox.query().equal(CacheEntity_.key, key).build().findUnique();
  if (entity!=null) {
    cacheEntityBox.remove(entity);
  }
}

代码示例来源:origin: fengzhizi715/RxCache4a

@Override
public <T> Record<T> retrieve(String key, Type type) {
  CacheEntity entity = cacheEntityBox.query().equal(CacheEntity_.key, key).build().findUnique();
  if (entity==null) return null;
  long timestamp = entity.timestamp;
  long expireTime = entity.expireTime;
  T result = null;
  if (expireTime<0) { // 缓存的数据从不过期
    String json = entity.data;
    result = converter.fromJson(json,type);
  } else {
    if (timestamp + expireTime > System.currentTimeMillis()) {  // 缓存的数据还没有过期
      String json = entity.data;
      result = converter.fromJson(json,type);
    } else {        // 缓存的数据已经过期
      evict(key);
    }
  }
  return result != null ? new Record<>(Source.PERSISTENCE, key, result, timestamp, expireTime) : null;
}

相关文章