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

The following examples show how to use com.j256.ormlite.dao.Dao#refresh() . 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: MappedRefreshTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testRefreshDouble() throws Exception {
	// don't try this at home folks

	// don't create the Foo class because it will be created by the FooNotId
	Dao<LocalFoo, Object> fooDao = createDao(LocalFoo.class, false);
	Dao<FooNotId, Object> fooNotIdDao = createDao(FooNotId.class, true);

	LocalFoo foo = new LocalFoo();
	foo.id = "foo";

	// create 1 of them which should work
	assertEquals(1, fooDao.create(foo));
	// refresh should work
	assertEquals(1, fooDao.refresh(foo));

	// behind the scenes, insert another into the foo table, EVIL!!
	FooNotId fooNotId = new FooNotId();
	fooNotId.id = "foo";

	assertEquals(1, fooNotIdDao.create(fooNotId));

	// refresh should not work becaue there are 2 classes which match this id
	fooDao.refresh(foo);
}
 
Example 2
Source File: VideoDetailActivity.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onUserUpdate(User user) {
	Log.d(LOG_TAG, "onUserUpdate");
	boolean loggedIn = user != null;
	
	// Look up or refresh the UserVideo.
	if (loggedIn && dataService != null) {
		try {
			Dao<UserVideo, Integer> userVideoDao = dataService.getHelper().getUserVideoDao();
			if (userVideo == null || userVideo.getUser() == null) {
				Map<String, Object> values = new HashMap<String, Object>();
				values.put("user_id", user.getNickname());
				values.put("video_id", video.getReadable_id());
				List<UserVideo> results = userVideoDao.queryForFieldValues(values);
				if (results.size() > 0) {
					Log.d(LOG_TAG, String.format("found %d results. setting first", results.size()));
					userVideo = results.get(0);
				}
			} else {
				userVideoDao.refresh(userVideo);
			}
			
			// Show the points badge.
			setPoints(userVideo.getPoints());
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
	} else {
		Log.d(LOG_TAG, String.format("user: %s, dataService: %s", loggedIn ? user.getNickname() : "null", dataService));
		// User just logged out (or we strangely lost db connectivity since the last update).
		userVideo = null;
	}
	
	
}
 
Example 3
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testRefreshNoId() throws Exception {
	Dao<NoId, Object> noIdDao = createDao(NoId.class, true);
	NoId noId = new NoId();
	noId.stuff = "1";
	assertEquals(1, noIdDao.create(noId));
	noIdDao.refresh(noId);
}
 
Example 4
Source File: BigIntegerTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDefaultValue() throws Exception {
	Dao<BigIntegerDefaultValue, Object> dao = createDao(BigIntegerDefaultValue.class, true);
	BigIntegerDefaultValue foo = new BigIntegerDefaultValue();
	dao.create(foo);

	assertNull(foo.bigInteger);
	dao.refresh(foo);
	assertEquals(new BigInteger(DEFAULT_VALUE), foo.bigInteger);
}
 
Example 5
Source File: UuidTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testUuidDefault() throws Exception {
	System.out.println(UUID.randomUUID().toString());

	Dao<UuidClassDefault, Object> dao = createDao(UuidClassDefault.class, true);
	UuidClassDefault foo = new UuidClassDefault();
	dao.create(foo);

	assertNull(foo.uuid);
	dao.refresh(foo);
	assertNotNull(foo.uuid);
	assertEquals(UUID.fromString(DEFAULT_VALUE), foo.uuid);
}
 
Example 6
Source File: UuidTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUuidInvalidDefault() throws Exception {
	Dao<UuidClassInvalidDefault, Object> dao = createDao(UuidClassInvalidDefault.class, true);
	UuidClassInvalidDefault foo = new UuidClassInvalidDefault();
	dao.create(foo);

	assertNull(foo.uuid);
	dao.refresh(foo);
}
 
Example 7
Source File: BigDecimalTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDefaultValue() throws Exception {
	Dao<BigDecimalDefaultValue, Object> dao = createDao(BigDecimalDefaultValue.class, true);
	BigDecimalDefaultValue foo = new BigDecimalDefaultValue();
	dao.create(foo);

	assertNull(foo.bigDecimal);
	dao.refresh(foo);
	assertEquals(new BigDecimal(DEFAULT_VALUE), foo.bigDecimal);
}
 
Example 8
Source File: DBAcl.java    From passopolis-server with GNU General Public License v3.0 4 votes vote down vote up
/** Refreshes the group object from the database and returns it. */
public DBGroup loadGroup(Dao<DBGroup, Integer> dao) throws SQLException {
  dao.refresh(group);
  return group;
}
 
Example 9
Source File: DBAcl.java    From passopolis-server with GNU General Public License v3.0 4 votes vote down vote up
public DBIdentity loadMemberIdentity(Dao<DBIdentity, Integer> dao) throws SQLException {
  dao.refresh(memberIdentity);
  return memberIdentity;
}
 
Example 10
Source File: DBAcl.java    From passopolis-server with GNU General Public License v3.0 4 votes vote down vote up
/**
 * returns the fully-loaded group.
 */
public DBGroup loadMemberGroup(Dao<DBGroup, Integer> dao) throws SQLException {
  dao.refresh(memberGroup);
  return memberGroup;
}
 
Example 11
Source File: JdbcQueryBuilderTest.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
@Test
public void testInnerJoin() throws Exception {
	Dao<Bar, Integer> barDao = createDao(Bar.class, true);
	Dao<Baz, Integer> bazDao = createDao(Baz.class, true);
	Dao<Bing, Integer> bingDao = createDao(Bing.class, true);

	Bar bar1 = new Bar();
	bar1.val = 2234;
	assertEquals(1, barDao.create(bar1));
	Bar bar2 = new Bar();
	bar2.val = 324322234;
	assertEquals(1, barDao.create(bar2));

	Baz baz1 = new Baz();
	baz1.bar = bar1;
	assertEquals(1, bazDao.create(baz1));
	Baz baz2 = new Baz();
	baz2.bar = bar2;
	assertEquals(1, bazDao.create(baz2));

	Bing bing1 = new Bing();
	bing1.baz = baz1;
	assertEquals(1, bingDao.create(bing1));
	Bing bing2 = new Bing();
	bing2.baz = baz2;
	assertEquals(1, bingDao.create(bing2));

	QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
	barQb.where().eq(Bar.VAL_FIELD, bar1.val);

	QueryBuilder<Baz, Integer> bazQb = bazDao.queryBuilder();
	assertEquals(2, bazQb.query().size());
	bazQb.join(barQb);

	List<Bing> results = bingDao.queryBuilder().join(bazQb).query();
	assertEquals(1, results.size());
	assertEquals(bing1.id, results.get(0).id);
	assertEquals(baz1.id, results.get(0).baz.id);
	bazDao.refresh(results.get(0).baz);
	assertEquals(bar1.id, results.get(0).baz.bar.id);
}
 
Example 12
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testInnerJoin() throws Exception {
	Dao<Bar, Integer> barDao = createDao(Bar.class, true);
	Dao<Baz, Integer> bazDao = createDao(Baz.class, true);
	Dao<Bing, Integer> bingDao = createDao(Bing.class, true);

	Bar bar1 = new Bar();
	bar1.val = 2234;
	assertEquals(1, barDao.create(bar1));
	Bar bar2 = new Bar();
	bar2.val = 324322234;
	assertEquals(1, barDao.create(bar2));

	Baz baz1 = new Baz();
	baz1.bar = bar1;
	assertEquals(1, bazDao.create(baz1));
	Baz baz2 = new Baz();
	baz2.bar = bar2;
	assertEquals(1, bazDao.create(baz2));

	Bing bing1 = new Bing();
	bing1.baz = baz1;
	assertEquals(1, bingDao.create(bing1));
	Bing bing2 = new Bing();
	bing2.baz = baz2;
	assertEquals(1, bingDao.create(bing2));

	QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
	barQb.where().eq(Bar.VAL_FIELD, bar1.val);

	QueryBuilder<Baz, Integer> bazQb = bazDao.queryBuilder();
	assertEquals(2, bazQb.query().size());
	bazQb.join(barQb);

	List<Bing> results = bingDao.queryBuilder().join(bazQb).query();
	assertEquals(1, results.size());
	assertEquals(bing1.id, results.get(0).id);
	assertEquals(baz1.id, results.get(0).baz.id);
	bazDao.refresh(results.get(0).baz);
	assertEquals(bar1.id, results.get(0).baz.bar.id);
}
 
Example 13
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testOrdersInMultiQueryBuilder() throws Exception {
	Dao<Bar, Integer> barDao = createDao(Bar.class, true);
	Dao<Baz, Integer> bazDao = createDao(Baz.class, true);
	Dao<Bing, Integer> bingDao = createDao(Bing.class, true);

	Bar bar1 = new Bar();
	bar1.val = 1;
	assertEquals(1, barDao.create(bar1));
	Bar bar2 = new Bar();
	bar2.val = 3;
	assertEquals(1, barDao.create(bar2));
	Bar bar3 = new Bar();
	bar3.val = 2;
	assertEquals(1, barDao.create(bar3));

	Baz baz1 = new Baz();
	baz1.bar = bar1;
	baz1.val = 2;
	assertEquals(1, bazDao.create(baz1));
	Baz baz2 = new Baz();
	baz2.bar = bar2;
	baz2.val = 2;
	assertEquals(1, bazDao.create(baz2));
	Baz baz3 = new Baz();
	baz3.bar = bar3;
	baz3.val = 1;
	assertEquals(1, bazDao.create(baz3));

	Bing bing1 = new Bing();
	bing1.baz = baz1;
	assertEquals(1, bingDao.create(bing1));
	Bing bing2 = new Bing();
	bing2.baz = baz2;
	assertEquals(1, bingDao.create(bing2));
	Bing bing3 = new Bing();
	bing3.baz = baz3;
	assertEquals(1, bingDao.create(bing3));

	QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
	barQb.orderBy(Bar.VAL_FIELD, true);

	QueryBuilder<Baz, Integer> bazQb = bazDao.queryBuilder();
	assertEquals(3, bazQb.query().size());
	bazQb.orderBy(Baz.VAL_FIELD, true);
	bazQb.join(barQb);

	List<Bing> results = bingDao.queryBuilder().join(bazQb).query();
	assertEquals(3, results.size());
	assertEquals(bing3.id, results.get(0).id);
	assertEquals(baz3.id, results.get(0).baz.id);
	bazDao.refresh(results.get(0).baz);
	assertEquals(bar3.id, results.get(0).baz.bar.id);
	bazDao.refresh(results.get(1).baz);
	assertEquals(bar1.id, results.get(1).baz.bar.id);
	bazDao.refresh(results.get(2).baz);
	assertEquals(bar2.id, results.get(2).baz.bar.id);
}