com.j256.ormlite.dao.Dao.queryForFirst()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(191)

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

Dao.queryForFirst介绍

[英]Query for and return the first item in the object table which matches the PreparedQuery. See #queryBuilder() for more information. This can be used to return the object that matches a single unique column. You should use #queryForId(Object) if you want to query for the id column.
[中]查询并返回对象表中与PreparedQuery匹配的第一项。有关详细信息,请参见#queryBuilder()。这可用于返回与单个唯一列匹配的对象。如果要查询id列,应使用#queryForId(Object)。

代码示例

代码示例来源:origin: j256/ormlite-core

/**
 * A short cut to {@link Dao#queryForFirst(PreparedQuery)}.
 */
public T queryForFirst() throws SQLException {
  return dao.queryForFirst(prepare());
}

代码示例来源:origin: j256/ormlite-core

/**
 * @see Dao#queryForFirst(PreparedQuery)
 */
@Override
public T queryForFirst(PreparedQuery<T> preparedQuery) {
  try {
    return dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    logMessage(e, "queryForFirst threw exception on: " + preparedQuery);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: com.j256.ormlite/ormlite-core

/**
 * @see Dao#queryForFirst(PreparedQuery)
 */
@Override
public T queryForFirst(PreparedQuery<T> preparedQuery) {
  try {
    return dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    logMessage(e, "queryForFirst threw exception on: " + preparedQuery);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: com.j256.ormlite/ormlite-core

/**
 * A short cut to {@link Dao#queryForFirst(PreparedQuery)}.
 */
public T queryForFirst() throws SQLException {
  return dao.queryForFirst(prepare());
}

代码示例来源:origin: j256/ormlite-core

@Test(expected = RuntimeException.class)
public void testQueryForFirstPreparedThrow() throws Exception {
  @SuppressWarnings("unchecked")
  Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  expect(dao.queryForFirst(null)).andThrow(new SQLException("Testing catch"));
  replay(dao);
  rtDao.queryForFirst(null);
  verify(dao);
}

代码示例来源:origin: QuickBlox/q-municate-android

public Dialog getByDialogId(String dialogId) {
  Dialog dialog = null;
  try {
    QueryBuilder<Dialog, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(Dialog.Column.ID, dialogId);
    PreparedQuery<Dialog> preparedQuery = queryBuilder.prepare();
    dialog = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return dialog;
}

代码示例来源:origin: QuickBlox/q-municate-android

public Attachment getByAttachmentType(Attachment.Type type) {
  Attachment attachment = null;
  try {
    QueryBuilder<Attachment, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(Attachment.Column.ID, type);
    PreparedQuery<Attachment> preparedQuery = queryBuilder.prepare();
    attachment = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return attachment;
}

代码示例来源:origin: QuickBlox/q-municate-android

public Social getBySocialId(String socialId) {
  Social social = null;
  try {
    QueryBuilder<Social, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(Social.Column.ID, socialId);
    PreparedQuery<Social> preparedQuery = queryBuilder.prepare();
    social = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return social;
}

代码示例来源:origin: QuickBlox/q-municate-android

public Dialog getByRoomJid(String roomJid) {
  Dialog dialog = null;
  try {
    QueryBuilder<Dialog, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(Dialog.Column.ROOM_JID, roomJid);
    PreparedQuery<Dialog> preparedQuery = queryBuilder.prepare();
    dialog = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return dialog;
}

代码示例来源:origin: QuickBlox/q-municate-android

public Message getByMessageId(String messageId) {
  Message message = null;
  try {
    QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();
    messageQueryBuilder.where().eq(Message.Column.ID, messageId);
    PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
    message = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return message;
}

代码示例来源:origin: QuickBlox/q-municate-android

public Friend getByUserId(int userId) {
  Friend friend = null;
  try {
    QueryBuilder<Friend, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(QMUserColumns.ID, userId);
    PreparedQuery<Friend> preparedQuery = queryBuilder.prepare();
    friend = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return friend;
}

代码示例来源:origin: QuickBlox/q-municate-android

@Override
public  QMUser getUserByColumn(String column, String value) {
  QMUser user = null;
  try {
    QueryBuilder<QMUser, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(column, value);
    PreparedQuery<QMUser> preparedQuery = queryBuilder.prepare();
    user = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return user;
}

代码示例来源:origin: QuickBlox/q-municate-android

public QMUser getUserRequestById(int userId) {
  UserRequest userRequest = null;
  try {
    QueryBuilder<UserRequest, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().eq(QMUserColumns.ID, userId);
    PreparedQuery<UserRequest> preparedQuery = queryBuilder.prepare();
    userRequest = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return userRequest != null ? userRequest.getUser() : null;
}

代码示例来源:origin: QuickBlox/q-municate-android

public Message getLastMessageWithTempByDialogId(List<Long> dialogOccupantsList) {
  Message message = null;
  try {
    QueryBuilder<Message, Long> queryBuilder = dao.queryBuilder();
    queryBuilder.where().in(DialogOccupant.Column.ID, dialogOccupantsList);
    queryBuilder.orderBy(Message.Column.CREATED_DATE, false);
    PreparedQuery<Message> preparedQuery = queryBuilder.prepare();
    message = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return message;
}

代码示例来源:origin: QuickBlox/q-municate-android

public DialogOccupant getDialogOccupant(String dialogId, int userId) {
  DialogOccupant dialogOccupant = null;
  try {
    QueryBuilder<DialogOccupant, Long> queryBuilder = dao.queryBuilder();
    Where<DialogOccupant, Long> where = queryBuilder.where();
    where.and(where.eq(Dialog.Column.ID, dialogId), where.eq(QMUserColumns.ID, userId));
    PreparedQuery<DialogOccupant> preparedQuery = queryBuilder.prepare();
    dialogOccupant = dao.queryForFirst(preparedQuery);
  } catch (SQLException e) {
    ErrorUtils.logError(e);
  }
  return dialogOccupant;
}

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

@Override
public MetricsBinaryTypeDevice get(MetricsBinaryTypeDevice metric) {
  try {
    return this.getDao().queryForFirst(
        this.getDao().queryBuilder()
            .where()
            .eq(MetricsBinaryTypeDevice.KEY_SENSOR_VARIABLE_ID, metric.getSensorVariable().getId())
            .and().eq(MetricsBinaryTypeDevice.KEY_TIMESTAMP, metric.getTimestamp()).prepare());
  } catch (SQLException ex) {
    _logger.error("unable to get, metric:{}", metric, ex);
  }
  return null;
}

代码示例来源:origin: mycontroller-org/mycontroller

@Override
public MetricsBinaryTypeDevice get(MetricsBinaryTypeDevice metric) {
  try {
    return this.getDao().queryForFirst(
        this.getDao().queryBuilder()
            .where()
            .eq(MetricsBinaryTypeDevice.KEY_SENSOR_VARIABLE_ID, metric.getSensorVariable().getId())
            .and().eq(MetricsBinaryTypeDevice.KEY_TIMESTAMP, metric.getTimestamp()).prepare());
  } catch (SQLException ex) {
    _logger.error("unable to get, metric:{}", metric, ex);
    throw new McDatabaseException(ex);
  }
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testQueryForFirstNoResults() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  assertNull(dao.queryForFirst(dao.queryBuilder().prepare()));
}

代码示例来源:origin: mycontroller-org/mycontroller

@Override
public MetricsBatteryUsage get(MetricsBatteryUsage metric) {
  try {
    return this.getDao().queryForFirst(
        this.getDao().queryBuilder()
            .where()
            .eq(MetricsBatteryUsage.KEY_NODE_ID, metric.getNode().getId())
            .and().eq(MetricsBatteryUsage.KEY_AGGREGATION_TYPE, metric.getAggregationType())
            .and().eq(MetricsBatteryUsage.KEY_TIMESTAMP, metric.getTimestamp()).prepare());
  } catch (SQLException ex) {
    _logger.error("unable to get, metric:{}", metric, ex);
    throw new McDatabaseException(ex);
  }
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testQueryForFirst() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo1 = new Foo();
  assertEquals(1, dao.create(foo1));
  Foo foo2 = new Foo();
  assertEquals(1, dao.create(foo2));
  assertNotNull(dao.queryForId(foo1.id));
  assertNotNull(dao.queryForId(foo2.id));
  assertEquals(2, dao.queryForAll().size());
  Foo foo3 = dao.queryForFirst(dao.queryBuilder().prepare());
  assertNotNull(foo2);
  assertEquals(foo1.id, foo3.id);
}

相关文章