Java Code Examples for com.j256.ormlite.dao.Dao#queryForId()

The following examples show how to use com.j256.ormlite.dao.Dao#queryForId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MappedCreateTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testCreateWithAllowGeneratedIdInsert() throws Exception {
	Dao<AllowGeneratedIdInsert, Integer> dao = createDao(AllowGeneratedIdInsert.class, true);
	AllowGeneratedIdInsert foo = new AllowGeneratedIdInsert();
	assertEquals(1, dao.create(foo));
	AllowGeneratedIdInsert result = dao.queryForId(foo.id);
	assertNotNull(result);
	assertEquals(foo.id, result.id);

	AllowGeneratedIdInsert foo2 = new AllowGeneratedIdInsert();
	assertEquals(1, dao.create(foo2));
	result = dao.queryForId(foo2.id);
	assertNotNull(result);
	assertEquals(foo2.id, result.id);
	assertFalse(foo2.id == foo.id);

	AllowGeneratedIdInsert foo3 = new AllowGeneratedIdInsert();
	foo3.id = 10002;
	assertEquals(1, dao.create(foo3));
	result = dao.queryForId(foo3.id);
	assertNotNull(result);
	assertEquals(foo3.id, result.id);
	assertFalse(foo3.id == foo.id);
	assertFalse(foo3.id == foo2.id);
}
 
Example 2
Source File: SetValueTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testBasic() throws Exception {
	Dao<Foo, Integer> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	int val = 66654654;
	foo.val = val;
	assertEquals(1, dao.create(foo));

	Foo result = dao.queryForId(foo.id);
	assertNotNull(result);
	assertEquals(val, result.val);

	UpdateBuilder<Foo, Integer> up = dao.updateBuilder();
	int newVal = 165445654;
	up.updateColumnValue(Foo.VAL_COLUMN_NAME, newVal);
	assertEquals(1, dao.update(up.prepare()));

	result = dao.queryForId(foo.id);
	assertNotNull(result);
	assertEquals(newVal, result.val);
}
 
Example 3
Source File: NoteManager.java    From ETSMobile-Android2 with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes marks in DB that doesn't exist on API
 *
 * @param
 */
private void deleteExpiredListeDesElementsEvaluation(String id) {
    DatabaseHelper dbHelper = new DatabaseHelper(context);

    try {

        Dao<ListeDesElementsEvaluation, String> listeDesElementsEvaluationDao = dbHelper.getDao(ListeDesElementsEvaluation.class);
        ListeDesElementsEvaluation listeDesElementsEvaluation = listeDesElementsEvaluationDao.queryForId(id);

        if (listeDesElementsEvaluation != null) {
            Dao<ElementEvaluation, String> elementsEvaluationDao = dbHelper.getDao(ElementEvaluation.class);
            DeleteBuilder<ElementEvaluation, String> deleteBuilder = elementsEvaluationDao.deleteBuilder();

            Where where = deleteBuilder.where();
            where.eq("listeDesElementsEvaluation_id", listeDesElementsEvaluation);

            deleteBuilder.delete();
        }

        listeDesElementsEvaluationDao.deleteById(id);


    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: MappedQueryForIdTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testTooManyFooId() throws Exception {
	Dao<LocalFoo, String> fooDao = createDao(LocalFoo.class, true);
	String stuff = "zing";
	LocalFoo foo = new LocalFoo();
	foo.id = "blah";
	foo.stuff = stuff;
	assertEquals(1, fooDao.create(foo));

	foo = new LocalFoo();
	foo.id = "ick";
	foo.stuff = stuff;
	assertEquals(1, fooDao.create(foo));

	// don't do this at home kiddies -- creates a different dao looking at the Foo table
	// it doesn't create the new table
	Dao<FakeFoo, String> fakeFooDao = createDao(FakeFoo.class, false);
	// this fails because >1 item is returned from an id search -- baaaaad
	fakeFooDao.queryForId(stuff);
}
 
Example 5
Source File: SetValueTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSerializable() throws Exception {
	Dao<TestSerial, Integer> dao = createDao(TestSerial.class, true);
	TestSerial foo = new TestSerial();
	String stuff = "hjrjpe";
	foo.stuff = stuff;
	assertEquals(1, dao.create(foo));

	TestSerial result = dao.queryForId(foo.id);
	assertNotNull(result);
	assertEquals(stuff, result.stuff);

	UpdateBuilder<TestSerial, Integer> up = dao.updateBuilder();
	String newStuff = "165445654";
	up.updateColumnValue("stuff", newStuff);
	assertEquals(1, dao.update(up.prepare()));

	result = dao.queryForId(foo.id);
	assertNotNull(result);
	assertEquals(newStuff, result.stuff);
}
 
Example 6
Source File: DatabaseConnectionProxyFactoryTest.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Here we inserting a particular val value and the connection-proxy should alter it _before_ it is inserted.
 */
@Test
public void testChangeInsertValue() throws Exception {
	Dao<Foo, Object> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	foo.val = TEST_CHANGE_FROM;

	ConnectionProxy.lastValue = 0;
	assertEquals(1, dao.create(foo));
	/*
	 * After we create an instance of foo, we check to see that our proxy was able to intercept the val argument.
	 */
	assertEquals(foo.val, ConnectionProxy.lastValue);

	Foo result = dao.queryForId(foo.id);
	assertNotNull(result);
	assertEquals(TEST_CHANGE_TO, result.val);
	assertTrue(result.val != TEST_CHANGE_FROM);
}
 
Example 7
Source File: AbstractListFragment.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 6 votes vote down vote up
/**
    * implements ObjectCallback<KADataService>
    */
@Override
public void call(KADataService service) {
	// Called when the service becomes available.
	
	if (topicId != null) {
        DatabaseHelper dbh = service.getHelper();
		try {
			Dao<Topic, String> topicDao = dbh.getTopicDao();
			topic = topicDao.queryForId(topicId);
			dao = dbh.getDao(getEntityClass());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	else {
		topic = service.getRootTopic();
		topicId = topic.getId();
	}

	resetListContents(topicId);
}
 
Example 8
Source File: UuidTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUuidString() throws Exception {
	Dao<UuidString, Integer> stringDao = createDao(UuidString.class, true);
	UuidString uuidString = new UuidString();
	uuidString.uuid = "not a valid uuid string";
	assertEquals(1, stringDao.create(uuidString));

	Dao<UuidClass, Integer> uuidDao = createDao(UuidClass.class, false);
	uuidDao.queryForId(uuidString.id);
}
 
Example 9
Source File: MappedCreateTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testReadOnly() throws Exception {
	Dao<ReadOnly, Integer> readOnlyDao = createDao(ReadOnly.class, true);
	Dao<ReadOnlyInsert, Integer> readOnlyInsertDao = createDao(ReadOnlyInsert.class, true);

	ReadOnly readOnly = new ReadOnly();
	readOnly.stuff = "fpweojfew";
	readOnly.readOnly = "read read and only read";
	assertEquals(1, readOnlyDao.create(readOnly));

	ReadOnly result = readOnlyDao.queryForId(readOnly.id);
	assertNotNull(result);
	assertEquals(readOnly.id, result.id);
	assertEquals(readOnly.stuff, result.stuff);
	// this is null because the above create didn't insert it
	assertNull(result.readOnly);

	ReadOnlyInsert insert = new ReadOnlyInsert();
	insert.stuff = "wefewerwrwe";
	insert.readOnly = "insert should work here";
	assertEquals(1, readOnlyInsertDao.create(insert));

	result = readOnlyDao.queryForId(insert.id);
	assertNotNull(result);
	assertEquals(insert.id, result.id);
	assertEquals(insert.stuff, result.stuff);
	// but this is not null because it was inserted using readOnlyInsertDao
	assertEquals(insert.readOnly, result.readOnly);

	ReadOnly update = result;
	update.readOnly = "something else";
	// the update should _not_ update read-only field
	assertEquals(1, readOnlyDao.update(update));

	result = readOnlyDao.queryForId(insert.id);
	assertFalse(update.readOnly.equals(result.readOnly));
}
 
Example 10
Source File: BaseDaoEnabledTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testUpdate() throws Exception {
	Dao<One, Integer> dao = createDao(One.class, true);
	One one = new One();
	String stuff1 = "fewpfjewfew";
	one.stuff = stuff1;
	assertEquals(1, dao.create(one));
	String stuff2 = "fjpfejpwewpfjewfew";
	one.stuff = stuff2;
	assertEquals(1, one.update());
	One one2 = dao.queryForId(one.id);
	assertEquals(stuff2, one2.stuff);
}
 
Example 11
Source File: MappedCreateTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCreateReserverdTable() throws Exception {
	Dao<Where, String> dao = createDao(Where.class, true);
	String id = "from-string";
	Where where = new Where();
	where.id = id;
	dao.create(where);
	Where where2 = dao.queryForId(id);
	assertEquals(id, where2.id);
	assertEquals(1, dao.delete(where2));
	assertNull(dao.queryForId(id));
}
 
Example 12
Source File: UserDao.java    From renrenpay-android with Apache License 2.0 5 votes vote down vote up
public static User findById(int id) {
    User user = null;
    try {
        Dao<User, Integer> dao = DatabaseHelper.getDbHelper().getDao(User.class);
        user = dao.queryForId(id);
    } catch(SQLException e) {
        e.printStackTrace();
    }
    return user;
}
 
Example 13
Source File: TableInfoTest.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Test to make sure that we can call a private constructor
 */
@Test
public void testPrivateConstructor() throws Exception {
	Dao<PrivateConstructor, Object> packConstDao = createDao(PrivateConstructor.class, true);
	int id = 12312321;
	PrivateConstructor pack1 = PrivateConstructor.makeOne(id);
	assertEquals(id, pack1.id);
	packConstDao.create(pack1);
	// we should be able to look it up
	PrivateConstructor pack2 = packConstDao.queryForId(id);
	// and the id should match
	assertEquals(id, pack2.id);
}
 
Example 14
Source File: DataPersisterManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCustomTypePersister() throws Exception {
	Dao<PersistedStoredPersister, Object> dao = createDao(PersistedStoredPersister.class, true);
	PersistedStoredPersister wrapper = new PersistedStoredPersister();
	String stuff = "pfjwpfjww";
	wrapper.storedClass = new StoredClass(stuff);
	assertEquals(1, dao.create(wrapper));

	PersistedStoredPersister wrapperResult = dao.queryForId(wrapper.id);
	assertNotNull(wrapperResult.storedClass);
	assertEquals(stuff, wrapperResult.storedClass.stuff);
}
 
Example 15
Source File: EnumIntegerTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDefaultValue() throws Exception {
	Dao<EnumDefault, Object> dao = createDao(EnumDefault.class, true);
	EnumDefault enumDefault = new EnumDefault();
	assertEquals(1, dao.create(enumDefault));
	EnumDefault result = dao.queryForId(enumDefault.id);
	assertNotNull(result);
	assertEquals(OurEnum.SECOND, result.ourEnum);
}
 
Example 16
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testNestedTransactions() throws Exception {
	final Dao<Foo, Integer> dao = createDao(Foo.class, true);
	final Foo foo = new Foo();
	assertEquals(1, dao.create(foo));

	Foo result = dao.queryForId(foo.id);
	assertNotNull(result);

	try {
		TransactionManager.callInTransaction(connectionSource, new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				TransactionManager.callInTransaction(connectionSource, new Callable<Void>() {
					@Override
					public Void call() throws Exception {
						dao.delete(foo);
						return null;
					}
				});
				throw new SQLException();
			}
		});
		fail("Should have thrown");
	} catch (SQLException se) {
		// expected
	}

	result = dao.queryForId(foo.id);
	assertNotNull(result);
}
 
Example 17
Source File: UuidTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testUuidDao() throws Exception {
	Dao<UuidClass, Integer> dao = createDao(UuidClass.class, true);
	UuidClass uuid = new UuidClass();
	uuid.uuid = null;
	assertEquals(1, dao.create(uuid));

	UuidClass uuidResult = dao.queryForId(uuid.id);
	assertNotNull(uuidResult);
	assertNull(uuidResult.uuid);
}
 
Example 18
Source File: DateStringTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testDateStringBackwardsCompatibility() throws Exception {
	Dao<VersionString, Object> stringDao = createDao(VersionString.class, true);
	Dao<VersionDate, Object> dateDao = createDao(VersionDate.class, true);

	VersionString string = new VersionString();
	/*
	 * WARNING, do not change this test otherwise you break backwards compatibility with string equality checking.
	 * 
	 * Changing this would _also_ break the {@code version=true} feature with dates which _also_ uses equality.
	 */
	final String formatStr = "yyyy-MM-dd HH:mm:ss.SSSSSS";
	final SimpleDateFormat format = new SimpleDateFormat(formatStr);
	Date date = new Date();
	string.date = format.format(date);
	assertEquals(1, stringDao.create(string));

	VersionDate result = dateDao.queryForId(string.id);
	assertNotNull(result);

	/*
	 * Check equality testing of dates.
	 */
	List<VersionDate> results = dateDao.queryForEq(DATE_COLUMN, result.date);
	assertNotNull(results);
	assertEquals(1, results.size());
	assertEquals(date, results.get(0).date);

	assertEquals(date, result.date);

	/*
	 * Check updating which was affected if the field is a version field.
	 */
	Thread.sleep(1);
	result.stuff = 12312312;
	assertEquals(1, dateDao.update(result));

	VersionDate newVersionDate = dateDao.queryForId(result.id);
	assertNotNull(newVersionDate);
	assertEquals(result.stuff, newVersionDate.stuff);
	assertTrue(newVersionDate.date.after(date));
}
 
Example 19
Source File: FieldTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testForeignConfigured() throws Exception {

	ArrayList<DatabaseFieldConfig> foreignFieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig fieldConfig = new DatabaseFieldConfig();
	fieldConfig.setFieldName("id");
	fieldConfig.setGeneratedId(true);
	foreignFieldConfigs.add(fieldConfig);
	fieldConfig = new DatabaseFieldConfig();
	fieldConfig.setFieldName("stuff");
	foreignFieldConfigs.add(fieldConfig);

	DatabaseTableConfig<ForeignObjectNoAnnotations> foreignTableConfig =
			new DatabaseTableConfig<ForeignObjectNoAnnotations>(databaseType, ForeignObjectNoAnnotations.class,
					foreignFieldConfigs);
	Dao<ForeignObjectNoAnnotations, Integer> foreignDao = createDao(foreignTableConfig, true);

	ArrayList<DatabaseFieldConfig> parentFieldConfigs = new ArrayList<DatabaseFieldConfig>();
	fieldConfig = new DatabaseFieldConfig();
	fieldConfig.setFieldName("id");
	fieldConfig.setGeneratedId(true);
	parentFieldConfigs.add(fieldConfig);
	fieldConfig = new DatabaseFieldConfig();
	fieldConfig.setFieldName("name");
	parentFieldConfigs.add(fieldConfig);
	fieldConfig = new DatabaseFieldConfig();
	fieldConfig.setFieldName("foreign");
	fieldConfig.setForeign(true);
	fieldConfig.setForeignTableConfig(foreignTableConfig);
	fieldConfig.setMaxForeignAutoRefreshLevel(2);
	parentFieldConfigs.add(fieldConfig);

	Dao<ObjectNoAnnotations, Integer> parentDao =
			createDao(new DatabaseTableConfig<ObjectNoAnnotations>(databaseType, ObjectNoAnnotations.class,
					parentFieldConfigs), true);

	ForeignObjectNoAnnotations foreign = new ForeignObjectNoAnnotations();
	foreign.stuff = "hello";
	foreignDao.create(foreign);

	ObjectNoAnnotations parent = new ObjectNoAnnotations();
	parent.name = "wow lookie";
	parent.foreign = foreign;
	parentDao.create(parent);

	ForeignObjectNoAnnotations foreignResult = foreignDao.queryForId(foreign.id);
	assertNotNull(foreignResult);
	assertNotSame(foreign, foreignResult);
	assertEquals(foreign.id, foreignResult.id);
	assertEquals(foreign.stuff, foreignResult.stuff);

	ObjectNoAnnotations parentResult = parentDao.queryForId(parent.id);
	assertNotNull(parentResult);
	assertEquals(parent.id, parentResult.id);
	assertEquals(parent.name, parentResult.name);
	assertNotNull(parentResult.foreign);
	assertNotSame(foreign, parentResult.foreign);
	assertEquals(foreign.id, parentResult.foreign.id);
	assertNull(parentResult.foreign.stuff);
}
 
Example 20
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 4 votes vote down vote up
private void testTransactionManager(TransactionManager mgr, final Exception exception,
		final Dao<Foo, Integer> fooDao) throws Exception {
	final Foo foo1 = new Foo();
	int val = 13131511;
	foo1.val = val;
	assertEquals(1, fooDao.create(foo1));
	try {
		final int ret = 13431231;
		int returned = mgr.callInTransaction(new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				// we delete it inside a transaction
				assertEquals(1, fooDao.delete(foo1));
				// we can't find it
				assertNull(fooDao.queryForId(foo1.id));
				if (exception != null) {
					// but then we throw an exception which rolls back the transaction
					throw exception;
				} else {
					return ret;
				}
			}
		});
		if (exception == null) {
			assertEquals(ret, returned);
		} else {
			fail("Should have thrown");
		}
	} catch (SQLException e) {
		if (exception == null) {
			throw e;
		} else {
			// expected
		}
	}

	if (exception == null) {
		// still doesn't find it after we delete it
		assertNull(fooDao.queryForId(foo1.id));
	} else {
		// still finds it after we delete it
		Foo foo2 = fooDao.queryForId(foo1.id);
		assertNotNull(foo2);
		assertEquals(val, foo2.val);
	}
}