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

The following examples show how to use com.j256.ormlite.dao.Dao#setObjectCache() . 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: FieldTypeTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testForeignInCache() throws Exception {
	Dao<ForeignParent, Integer> parentDao = createDao(ForeignParent.class, true);
	Dao<ForeignForeign, Integer> foreignDao = createDao(ForeignForeign.class, true);
	foreignDao.setObjectCache(true);

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

	assertSame(foreign, foreignDao.queryForId(foreign.id));

	ForeignParent parent = new ForeignParent();
	parent.foreign = foreign;
	parentDao.create(parent);

	ForeignParent result = parentDao.queryForId(parent.id);
	assertNotSame(parent, result);
	assertSame(foreign, result.foreign);
}
 
Example 2
Source File: Manager.java    From passopolis-server with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
public void enableCaches() throws SQLException {
  // Enable the object cache, which makes eager fetching significantly faster
  // TODO: This may have bad effects when combined with transactions!
  // Currently we throw out the Manager at the end of the transaction, so this
  // is okay
  // TODO: Revisit after we disable eager fetching on everything.
  if (false) {
    Dao<?, ?>[] daos = { groupDao, aclDao, identityDao, svsDao,
        groupSecretDao };
    for (Dao<?, ?> dao : daos) {
      dao.setObjectCache(true);
    }
  }
}
 
Example 3
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testInnerQueryCacheLookup() throws Exception {
	Dao<Foo, Object> fooDao = createDao(Foo.class, true);
	Dao<Outer, Object> outerDao = createDao(Outer.class, true);
	outerDao.setObjectCache(true);
	Dao<Inner, Object> innerDao = createDao(Inner.class, true);
	innerDao.setObjectCache(true);

	Foo foo = new Foo();
	assertEquals(1, fooDao.create(foo));

	Outer outer1 = new Outer();
	assertEquals(1, outerDao.create(outer1));
	Outer outer2 = new Outer();
	outer2.foreign = foo;
	assertEquals(1, outerDao.create(outer2));

	Inner inner = new Inner();
	inner.foreign = foo;
	assertEquals(1, innerDao.create(inner));

	QueryBuilder<Inner, Object> innerQb = innerDao.queryBuilder();
	innerQb.selectColumns(Inner.SOME_STRING_FIELD_NAME);
	QueryBuilder<Outer, Object> qb = outerDao.queryBuilder();
	List<Outer> results = qb.selectColumns(Outer.SOME_STRING_FIELD_NAME)
			.where()
			.in(Outer.SOME_STRING_FIELD_NAME, innerQb)
			.query();
	assertEquals(1, results.size());
}
 
Example 4
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCacheWithSelectColumns() throws Exception {
	Dao<CacheWithSelectColumns, Object> dao = createDao(CacheWithSelectColumns.class, true);
	// Add object to database
	CacheWithSelectColumns foo = new CacheWithSelectColumns();
	foo.text = "some text";
	dao.create(foo);
	// enable object cache
	dao.setObjectCache(true);

	// fetch the object, but only with its id field
	QueryBuilder<CacheWithSelectColumns, Object> qb =
			dao.queryBuilder().selectColumns(CacheWithSelectColumns.COLUMN_NAME_ID);
	CacheWithSelectColumns result = qb.queryForFirst();
	assertNotNull(result);
	assertNull(result.text);

	// fetch the same object again, this time asking for the text column as well
	qb = dao.queryBuilder().selectColumns(CacheWithSelectColumns.COLUMN_NAME_ID,
			CacheWithSelectColumns.COLUMN_NAME_TEXT);
	result = qb.queryForFirst();
	assertNotNull(result);
	assertEquals(foo.text, result.text);

	// fetch the same object again, this time asking for everything
	qb = dao.queryBuilder();
	result = qb.queryForFirst();
	assertNotNull(result);
	assertEquals(foo.text, result.text);
}