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

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

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

Dao.createOrUpdate介绍

[英]This is a convenience method for creating an item in the database if it does not exist. The id is extracted from the data parameter and a query-by-id is made on the database. If a row in the database with the same id exists then all of the columns in the database will be updated from the fields in the data parameter. If the id is null (or 0 or some other default value) or doesn't exist in the database then the object will be created in the database. This also means that your data item must have an id field defined.

NOTE: This method is synchronized because otherwise race conditions would be encountered if this is used by multiple threads.
[中]这是一种方便的方法,用于在数据库中创建不存在的项。从数据参数中提取id,并在数据库中按id进行查询。如果数据库中存在具有相同id的行,则数据库中的所有列都将从数据参数中的字段更新。如果id为null(或0或其他默认值)或数据库中不存在,则将在数据库中创建该对象。这也意味着您的数据项必须定义一个id字段。
注意:此方法是同步的,因为如果多个线程使用此方法,则会遇到竞争条件。

代码示例

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

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

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

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

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

public void createOrUpdateCacheEntryInDatabase(CacheEntry cacheEntry) throws SQLException {
  if (cacheEntry != null) {
    Dao<CacheEntry, ?> dao = getDao(CacheEntry.class);
    dao.createOrUpdate(cacheEntry);
  }
}

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

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

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

@Override
public void createOrUpdate(Object object) {
  try {
    dao.createOrUpdate((T) object);
    notifyObservers(OBSERVE_KEY);
  } catch (SQLException e) {
    ErrorUtils.logError(TAG, "createOrUpdateAll(Object) - " + e.getMessage());
  }
}

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

@Override
public void createOrUpdate(Object object, boolean notify) {
  try {
    Dao.CreateOrUpdateStatus status = dao.createOrUpdate((T) object);
    if (notify) {
      notifyObservers((T) object, status.isCreated() ? CREATE_ACTION : UPDATE_ACTION);
    }
  } catch (SQLException e) {
    ErrorUtils.logError(TAG, "createOrUpdate(Object) - " + e.getMessage());
  }
}

代码示例来源:origin: annmuor/jnode

public void saveOrUpdate(T object) {
  try {
    getDao().createOrUpdate(object);
  } catch (SQLException e) {
    logger.l1("SQL Exception in saveOrUpdate", e);
    logger.l1(MessageFormat.format("we worked with {0}", object));
  }
}

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

@Override
public void createOrUpdate(ResourcesGroup resourcesGroup) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(resourcesGroup);
    _logger.debug("CreateOrUpdate ResourcesGroup:[{}],Create:{},Update:{},Lines Changed:{}", resourcesGroup,
        status.isCreated(),
        status.isUpdated(), status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate ResourcesGroup:[{}]", resourcesGroup, ex);
  }
}

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

public void createOrUpdate(Tdao tdao) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(tdao);
    _logger.debug("CreateOrUpdate item:[{}],Create:{},Update:{},Lines Changed:{}",
        tdao, status.isCreated(), status.isUpdated(),
        status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate item:[{}]", tdao, ex);
  }
}

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

@Override
public void createOrUpdate(ResourcesGroupMap resourcesGroupMap) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(resourcesGroupMap);
    _logger.debug("CreateOrUpdate ResourcesGroupMap:[{}],Create:{},Update:{},Lines Changed:{}",
        resourcesGroupMap,
        status.isCreated(),
        status.isUpdated(), status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate ResourcesGroup:[{}]", resourcesGroupMap, ex);
  }
}

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

@Override
public void createOrUpdate(UserSettings userSettings) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(userSettings);
    _logger.debug("CreateOrUpdate UserSettings:[{}],Create:{},Update:{},Lines Changed:{}",
        userSettings, status.isCreated(), status.isUpdated(),
        status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate UserSettings:[{}]", userSettings, ex);
  }
}

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

@Override
public void createOrUpdate(UserSettings userSettings) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(userSettings);
    _logger.debug("CreateOrUpdate UserSettings:[{}],Create:{},Update:{},Lines Changed:{}",
        userSettings, status.isCreated(), status.isUpdated(),
        status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate UserSettings:[{}]", userSettings, ex);
    throw new McDatabaseException(ex);
  }
}

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

@Override
public void createOrUpdate(ResourcesGroup resourcesGroup) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(resourcesGroup);
    _logger.debug("CreateOrUpdate ResourcesGroup:[{}],Create:{},Update:{},Lines Changed:{}", resourcesGroup,
        status.isCreated(),
        status.isUpdated(), status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate ResourcesGroup:[{}]", resourcesGroup, ex);
    throw new McDatabaseException(ex);
  }
}

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

public void createOrUpdate(Tdao tdao) {
  try {
    CreateOrUpdateStatus status = this.getDao().createOrUpdate(tdao);
    _logger.debug("CreateOrUpdate item:[{}],Create:{},Update:{},Lines Changed:{}",
        tdao, status.isCreated(), status.isUpdated(),
        status.getNumLinesChanged());
  } catch (SQLException ex) {
    _logger.error("unable to CreateOrUpdate item:[{}]", tdao, ex);
    throw new McDatabaseException(ex);
  }
}

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

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

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

/**
 * Saves a single module data
 */
public boolean saveModuleData(ModuleData moduleData) throws SQLException {
  getModuleData(moduleData.getModule().getId(), moduleData.getName()).ifPresent((data) -> {
    moduleData.setId(data.getId());
  });
  return DB.MODULE_DATA_DAO.createOrUpdate(moduleData).getNumLinesChanged() == 1;
}

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

@Test
public void shouldStoreEntityWithStringIdAndLoadFromDbOnCreateOrUpdate() throws Exception {
  Dao<EntityWithStringId, String> dao = createDao(EntityWithStringId.class, true);
  EntityWithStringId data = new EntityWithStringId();
  data.id = "generated_id_from_factory";
  data.value = "some value";
  dao.createOrUpdate(data);
  EntityWithStringId found = dao.queryForId(data.id);
  assertNotNull(found);
  assertEquals(found.id, data.id);
  assertEquals(found.value, data.value);
}

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

@Test
public void testCreateOrUpdateNull() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  CreateOrUpdateStatus status = dao.createOrUpdate(null);
  assertFalse(status.isCreated());
  assertFalse(status.isUpdated());
  assertEquals(0, status.getNumLinesChanged());
}

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

@Test
public void shouldStoreEntityWithCustomTypeIdAndLoadFromDbOnCreateOrUpdate() throws Exception {
  Dao<EntityWithCustomTypeId, EntityId> dao = createDao(EntityWithCustomTypeId.class, true);
  EntityWithCustomTypeId data = new EntityWithCustomTypeId();
  data.id = EntityId.entityId("generated_id_from_factory");
  data.value = "some value";
  dao.createOrUpdate(data);
  EntityWithCustomTypeId found = dao.queryForId(data.id);
  assertNotNull(found);
  assertEquals(found.id, data.id);
  assertEquals(found.value, data.value);
}

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

@Test
public void testCreateOrUpdateNull() throws Exception {
  Dao<Foo, String> dao = createDao(Foo.class, true);
  CreateOrUpdateStatus status = dao.createOrUpdate(null);
  assertFalse(status.isCreated());
  assertFalse(status.isUpdated());
  assertEquals(0, status.getNumLinesChanged());
}

相关文章