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

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

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

Dao.createIfNotExists介绍

[英]This is a convenience method to creating a data item but only if the ID does not already exist in the table. This extracts the id from the data parameter, does a #queryForId(Object) on it, returning the data if it exists. If it does not exist #create(Object) will be called with the parameter.

NOTE: This method is synchronized because otherwise race conditions would be encountered if this is used by multiple threads.
[中]这是创建数据项的一种方便方法,但仅当表中不存在ID时。这将从数据参数中提取id,对其执行#queryForId(对象),如果数据存在,则返回数据。如果不存在#将使用参数调用create(Object)。
注意:此方法是同步的,因为如果多个线程使用此方法,则会遇到竞争条件。

代码示例

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

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

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

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

代码示例来源:origin: com.octo.android.robospice/robospice-ormlite

public <T> void createInDatabaseIfNotExist(T modelObject, Class<T> modelObjectClass) throws SQLException {
  if (modelObject != null) {
    Dao<T, ?> dao = getDao(modelObjectClass);
    dao.createIfNotExists(modelObject);
  }
}

代码示例来源:origin: geeksonsecurity/android-overlay-protection

final String applicationName = pm.getApplicationLabel(packageInfo.applicationInfo).toString();
app.setAppName(applicationName);
suspectedAppDao.createIfNotExists(app);

代码示例来源:origin: zulip/zulip-android

public void processStreams(List<EventsBranch> events) {
  for (EventsBranch event : events) {
    StreamWrapper streamEvent = (StreamWrapper) event;
    if (streamEvent.getOperation().equalsIgnoreCase(StreamWrapper.OP_OCCUPY)) {
      Dao<Stream, Integer> streamDao = app.getDao(Stream.class);
      List<Stream> streams = streamEvent.getStreams();
      for (Stream stream : streams) {
        try {
          // use default color for not subscribed streams
          stream.setDefaultColor();
          streamDao.createIfNotExists(stream);
        } catch (SQLException e) {
          ZLog.logException(e);
        }
      }
    } else {
      // TODO: handle other operations of stream event
      Log.d("AsyncEvents", "unhandled operation for stream type event");
      return;
    }
  }
}

代码示例来源:origin: geeksonsecurity/android-overlay-protection

if (suspectedAppDao != null) {
  try {
    suspectedAppDao.createIfNotExists(app);
  } catch (SQLException e) {
    Log.e(TAG, "Failed to create suspected app " + app.getPackageName(), e);

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

@Test(expected = RuntimeException.class)
public void testCreateIfNotExistsThrow() 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.createIfNotExists(null)).andThrow(new SQLException("Testing catch"));
  replay(dao);
  rtDao.createIfNotExists(null);
  verify(dao);
}

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

@Test
public void testCreateIfNotExistsNull() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  assertNull(dao.createIfNotExists(null));
}

代码示例来源:origin: lamarios/Homedash2

DB.SETTINGS_DAO.createIfNotExists(setting);
} catch (SQLException e) {
  logger.error("Error while creating setting", e);

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

@Test
public void testCreateIfNotExistsNull() throws Exception {
  Dao<Foo, String> dao = createDao(Foo.class, true);
  assertNull(dao.createIfNotExists(null));
}

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

@Test
public void testCreateIfNotExists() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo1 = new Foo();
  foo1.equal = 198412893;
  Foo fooResult = dao.createIfNotExists(foo1);
  assertSame(foo1, fooResult);
  // now if we do it again, we should get the database copy of foo
  fooResult = dao.createIfNotExists(foo1);
  assertNotSame(foo1, fooResult);
  assertEquals(foo1.id, fooResult.id);
  assertEquals(foo1.equal, fooResult.equal);
}

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

@Test
public void testCreateIfNotExists() throws Exception {
  Dao<Foo, String> dao = createDao(Foo.class, true);
  Foo foo1 = new Foo();
  String stuff = "stuff";
  foo1.stuff = stuff;
  Foo fooResult = dao.createIfNotExists(foo1);
  assertSame(foo1, fooResult);
  // now if we do it again, we should get the database copy of foo
  fooResult = dao.createIfNotExists(foo1);
  assertNotSame(foo1, fooResult);
  assertEquals(foo1.id, fooResult.id);
  assertEquals(foo1.stuff, fooResult.stuff);
}

代码示例来源:origin: lamarios/Homedash2

/**
 * Create default data like layouts and the main page
 */
public static void createDefaultData() throws SQLException {
  if (DB.LAYOUT_DAO.queryForAll().isEmpty()) {
    logger.info("Creating first page if it doesn't exist");
    Page page = new Page();
    page.setId(1);
    page.setName("Main");
    DB.PAGE_DAO.createIfNotExists(page);
    logger.info("Creating the 3 default layouts");
    Layout desktop = new Layout();
    desktop.setId(1);
    desktop.setMaxGridWidth(11);
    desktop.setName("Desktop");
    DB.LAYOUT_DAO.createOrUpdate(desktop);
    Layout tablet = new Layout();
    tablet.setId(2);
    tablet.setMaxGridWidth(8);
    tablet.setName("Tablet");
    DB.LAYOUT_DAO.createOrUpdate(tablet);
    Layout mobile = new Layout();
    mobile.setId(3);
    mobile.setMaxGridWidth(3);
    mobile.setName("Mobile");
    DB.LAYOUT_DAO.createOrUpdate(mobile);
  }
}

相关文章