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

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

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

Dao.create介绍

[英]Create a new row in the database from an object. If the object being created uses DatabaseField#generatedId() then the data parameter will be modified and set with the corresponding id from the database.
[中]从对象在数据库中创建新行。如果正在创建的对象使用DatabaseField#generatedId(),则将修改数据参数并使用数据库中相应的id进行设置。

代码示例

代码示例来源:origin: magefree/mage

expansionDao.callBatchTasks(() -> {
  if (newSets != null && newSets.size() > 0) {
    logger.info("DB: need to add " + newSets.size() + " new sets");
    try {
      for (ExpansionInfo exp : newSets) {
        expansionDao.create(exp);
  if (updatedSets != null && updatedSets.size() > 0) {
    logger.info("DB: need to update " + updatedSets.size() + " sets");
    try {

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

@Test
public void testDateStringFormat() throws Exception {
  Dao<DateStringFormat, Object> dao = createDao(DateStringFormat.class, true);
  DateStringFormat dateStringFormat = new DateStringFormat();
  dateStringFormat.date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-09-01");
  assertEquals(1, dao.create(dateStringFormat));
  List<DateStringFormat> results = dao.queryForAll();
  assertEquals(1, results.size());
  assertEquals(dateStringFormat.date, results.get(0).date);
}

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

@Test
public void testDefaultValue() throws Exception {
  Dao<EnumDefault, Object> dao = createDao(EnumDefault.class, true);
  EnumDefault enumDefault = new EnumDefault();
  assertEquals(1, dao.create(enumDefault));
  List<EnumDefault> unknowns = dao.queryForAll();
  assertEquals(1, unknowns.size());
  assertEquals(OurEnum.SECOND, unknowns.get(0).ourEnum);
}

代码示例来源:origin: magefree/mage

public void saveCards(final List<CardInfo> newCards, long newContentVersion) {
  try {
    cardDao.callBatchTasks(() -> {
      // add
      if (newCards != null && newCards.size() > 0) {
        logger.info("DB: need to add " + newCards.size() + " new cards");
        try {
          for (CardInfo card : newCards) {
            cardDao.create(card);
            if (classNames != null) {
              classNames.add(card.getClassName());
            }
          }
        } catch (SQLException ex) {
          Logger.getLogger(CardRepository.class).error("Error adding cards to DB - ", ex);
        }
      }
      // no card updates
      return null;
    });
    setContentVersion(newContentVersion);
    eventSource.fireRepositoryDbUpdated();
  } catch (Exception ex) {
    //
  }
}

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

@Test
public void testDefaultValue() throws Exception {
  Dao<DefaultValue, Object> defValDao = createDao(DefaultValue.class, true);
  DefaultValue defVal1 = new DefaultValue();
  assertEquals(1, defValDao.create(defVal1));
  List<DefaultValue> defValList = defValDao.queryForAll();
  assertEquals(1, defValList.size());
  DefaultValue defVal2 = defValList.get(0);
  assertEquals(DEFAULT_VALUE, (int) defVal2.intField);
}

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

@Test
public void testStringEnum() throws Exception {
  Dao<LocalEnumString, Object> fooDao = createDao(LocalEnumString.class, true);
  OurEnum ourEnum = OurEnum.SECOND;
  LocalEnumString foo = new LocalEnumString();
  foo.ourEnum = ourEnum;
  assertEquals(1, fooDao.create(foo));
  List<LocalEnumString> fooList = fooDao.queryForAll();
  assertEquals(1, fooList.size());
  assertEquals(ourEnum, fooList.get(0).ourEnum);
}

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

@Test
public void testIntEnum() throws Exception {
  Dao<LocalEnumInt, Object> fooDao = createDao(LocalEnumInt.class, true);
  OurEnum ourEnum = OurEnum.SECOND;
  LocalEnumInt foo = new LocalEnumInt();
  foo.ourEnum = ourEnum;
  assertEquals(1, fooDao.create(foo));
  List<LocalEnumInt> fooList = fooDao.queryForAll();
  assertEquals(1, fooList.size());
  assertEquals(ourEnum, fooList.get(0).ourEnum);
}

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

@Test
public void testEnumStringCreateGet() throws Exception {
  Class<LocalEnumToString> clazz = LocalEnumToString.class;
  Dao<LocalEnumToString, Object> dao = createDao(clazz, true);
  LocalEnumToString foo1 = new LocalEnumToString();
  foo1.ourEnum = OurEnum.FIRST;
  assertEquals(1, dao.create(foo1));
  LocalEnumToString foo2 = new LocalEnumToString();
  foo2.ourEnum = OurEnum.SECOND;
  assertEquals(1, dao.create(foo2));
  List<LocalEnumToString> results = dao.queryForAll();
  assertEquals(2, results.size());
  assertEquals(foo1.ourEnum, results.get(0).ourEnum);
  assertEquals(foo2.ourEnum, results.get(1).ourEnum);
}

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

@Test
public void testDateStringFormatNotDayAlign() throws Exception {
  Dao<DateStringFormat, Object> dao = createDao(DateStringFormat.class, true);
  DateStringFormat dateStringFormat = new DateStringFormat();
  dateStringFormat.date = new SimpleDateFormat("yyyy-MM-dd HH").parse("2012-09-01 12");
  assertEquals(1, dao.create(dateStringFormat));
  List<DateStringFormat> results = dao.queryForAll();
  assertEquals(1, results.size());
  assertFalse(dateStringFormat.date.equals(results.get(0).date));
}

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

@Test
public void testDateIntegerSeconds() throws Exception {
  Dao<LocalDateInteger, Object> dao = createDao(LocalDateInteger.class, true);
  long now = System.currentTimeMillis();
  Date val = new Date(now - (now % 1000) + 1 /* 1 extra ms here */);
  LocalDateInteger foo = new LocalDateInteger();
  foo.date = val;
  assertEquals(1, dao.create(foo));
  List<LocalDateInteger> results = dao.queryForAll();
  assertNotNull(results);
  assertEquals(1, results.size());
  // this is always false because the above date has some millis
  assertFalse(results.get(0).date.equals(val));
  assertEquals(new Date(now - (now % 1000)), results.get(0).date);
}

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

@Test
public void testRecursiveForeign() throws Exception {
  Dao<Recursive, Integer> recursiveDao = createDao(Recursive.class, true);
  Recursive recursive1 = new Recursive();
  Recursive recursive2 = new Recursive();
  recursive2.foreign = recursive1;
  assertEquals(recursiveDao.create(recursive1), 1);
  assertEquals(recursiveDao.create(recursive2), 1);
  Recursive recursive3 = recursiveDao.queryForId(recursive2.id);
  assertNotNull(recursive3);
  assertEquals(recursive1.id, recursive3.foreign.id);
}

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

@Test
public void testBaseClassAnnotations() throws Exception {
  Sub sub = new Sub();
  String stuff = "djeqpodjewdopjed";
  sub.stuff = stuff;
  Dao<Sub, Object> dao = createDao(Sub.class, true);
  assertEquals(0, sub.id);
  assertEquals(1, dao.create(sub));
  Sub sub2 = dao.queryForId(sub.id);
  assertNotNull(sub2);
  assertEquals(sub.stuff, sub2.stuff);
}

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

@Test
public void testQueryForEq() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  assertEquals(0, dao.countOf());
  Foo foo1 = new Foo();
  foo1.val = 1231231;
  assertEquals(1, dao.create(foo1));
  Foo foo2 = new Foo();
  foo2.val = foo1.val + 1;
  assertEquals(1, dao.create(foo2));
  List<Foo> results = dao.queryForEq(Foo.VAL_COLUMN_NAME, foo1.val);
  assertEquals(1, results.size());
  assertEquals(foo1.id, results.get(0).id);
}

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

@Test
public void testUnknownValueAnnotation() throws Exception {
  Dao<LocalUnknownEnum, Object> dao = createDao(LocalUnknownEnum.class, true);
  LocalUnknownEnum localUnknownEnum = new LocalUnknownEnum();
  localUnknownEnum.ourEnum = OurEnum.SECOND;
  assertEquals(1, dao.create(localUnknownEnum));
  assertEquals(1, dao.updateRaw("UPDATE Foo set ourEnum = 'THIRD'"));
  List<LocalUnknownEnum> unknowns = dao.queryForAll();
  assertEquals(1, unknowns.size());
  assertEquals(OurEnum.FIRST, unknowns.get(0).ourEnum);
}

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

@Test
public void testUuidInsertQuery() throws Exception {
  Dao<UuidGeneratedId, UUID> dao = createDao(UuidGeneratedId.class, true);
  UuidGeneratedId uuid = new UuidGeneratedId();
  String stuff = "fopewfjefjwgw";
  uuid.stuff = stuff;
  assertEquals(1, dao.create(uuid));
  UuidGeneratedId uuid2 = dao.queryForId(uuid.id);
  assertNotNull(uuid2);
  assertEquals(uuid.id, uuid2.id);
  assertEquals(stuff, uuid2.stuff);
}

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

@Test
public void testLongPrimitiveNull() throws Exception {
  Dao<LocalLongObj, Object> objDao = createDao(LocalLongObj.class, true);
  LocalLongObj foo = new LocalLongObj();
  foo.longField = null;
  assertEquals(1, objDao.create(foo));
  Dao<LocalLong, Object> dao = createDao(LocalLong.class, false);
  List<LocalLong> all = dao.queryForAll();
  assertEquals(1, all.size());
  assertEquals(0, all.get(0).longField);
}

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

@Test
public void testIntPrimitiveNull() throws Exception {
  Dao<LocalIntObj, Object> objDao = createDao(LocalIntObj.class, true);
  LocalIntObj foo = new LocalIntObj();
  foo.intField = null;
  assertEquals(1, objDao.create(foo));
  // overlapping table
  Dao<LocalInt, Object> dao = createDao(LocalInt.class, false);
  List<LocalInt> all = dao.queryForAll();
  assertEquals(1, all.size());
  assertEquals(0, all.get(0).intField);
}

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

@Test
public void testShortPrimitiveNull() throws Exception {
  Dao<LocalShortObj, Object> objDao = createDao(LocalShortObj.class, true);
  LocalShortObj foo = new LocalShortObj();
  foo.shortField = null;
  assertEquals(1, objDao.create(foo));
  Dao<LocalShort, Object> dao = createDao(LocalShort.class, false);
  List<LocalShort> all = dao.queryForAll();
  assertEquals(1, all.size());
  assertEquals(0, all.get(0).shortField);
}

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

@Test
public void testBytePrimitiveNull() throws Exception {
  Dao<LocalByteObj, Object> objDao = createDao(LocalByteObj.class, true);
  LocalByteObj foo = new LocalByteObj();
  foo.byteField = null;
  assertEquals(1, objDao.create(foo));
  Dao<LocalByte, Object> dao = createDao(LocalByte.class, false);
  List<LocalByte> all = dao.queryForAll();
  assertEquals(1, all.size());
  assertEquals(0, all.get(0).byteField);
}

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

@Test
public void testQueryPrepared() 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));
  Where<Foo, Integer> qb = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id);
  List<Foo> results = dao.query(qb.prepare());
  assertEquals(1, results.size());
  assertEquals(foo2.id, results.get(0).id);
}

相关文章