Java Code Examples for com.j256.ormlite.table.DatabaseTableConfig#fromClass()

The following examples show how to use com.j256.ormlite.table.DatabaseTableConfig#fromClass() . 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: DaoManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSelfReferenceWithLoadedConfig() throws Exception {
	DaoManager.clearCache();
	/*
	 * If a class was loaded as a config (this was found under Android) then, when it went recursive it would build
	 * itself and set its foreign field to be a primitive. Then when it re-configured itself it would scream because
	 * the primitive was marked as foreign.
	 * 
	 * The answer was to do a better job of pre-caching the DAOs in the DaoManager.
	 */
	DatabaseTableConfig<SelfReference> config = DatabaseTableConfig.fromClass(databaseType, SelfReference.class);
	@SuppressWarnings("unchecked")
	List<DatabaseTableConfig<?>> configs = new ArrayList<DatabaseTableConfig<?>>(Arrays.asList(config));
	DaoManager.addCachedDatabaseConfigs(configs);
	// this used to throw an exception
	DaoManager.createDao(connectionSource, SelfReference.class);
}
 
Example 2
Source File: DaoManagerTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testMoreComplexClassLoopWithLoadedConfig() throws Exception {
	DaoManager.clearCache();
	DatabaseTableConfig<MoreComplexLoopOne> config1 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopOne.class);
	DatabaseTableConfig<MoreComplexLoopTwo> config2 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopTwo.class);
	DatabaseTableConfig<MoreComplexLoopThree> config3 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopThree.class);
	DatabaseTableConfig<MoreComplexLoopFour> config4 =
			DatabaseTableConfig.fromClass(databaseType, MoreComplexLoopFour.class);
	@SuppressWarnings("unchecked")
	List<DatabaseTableConfig<?>> configs =
			new ArrayList<DatabaseTableConfig<?>>(Arrays.asList(config1, config2, config3, config4));
	DaoManager.addCachedDatabaseConfigs(configs);
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopOne.class));
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopTwo.class));
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopThree.class));
	assertNotNull(DaoManager.createDao(connectionSource, MoreComplexLoopFour.class));
}
 
Example 3
Source File: BaseCoreTest.java    From ormlite-core with ISC License 6 votes vote down vote up
private <T, ID> Dao<T, ID> configDao(BaseDaoImpl<T, ID> dao, boolean createTable) throws SQLException {
	if (connectionSource == null) {
		throw new SQLException("Connection source is null");
	}
	if (createTable) {
		DatabaseTableConfig<T> tableConfig = dao.getTableConfig();
		if (tableConfig == null) {
			tableConfig = DatabaseTableConfig.fromClass(databaseType, dao.getDataClass());
		}
		if (tableConfig.getSchemaName() != null && tableConfig.getSchemaName().length() > 0){
			createSchema(tableConfig);
		}
		createTable(tableConfig, true);
	}
	return dao;
}
 
Example 4
Source File: TableCreator.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
/**
 * If you are using the Spring type wiring, this should be called after all of the set methods.
 */
public void initialize() throws SQLException {
	if (!Boolean.parseBoolean(System.getProperty(AUTO_CREATE_TABLES))) {
		return;
	}

	if (configuredDaos == null) {
		throw new SQLException("configuredDaos was not set in " + getClass().getSimpleName());
	}

	// find all of the daos and create the tables
	for (Dao<?, ?> dao : configuredDaos) {
		Class<?> clazz = dao.getDataClass();
		try {
			DatabaseTableConfig<?> tableConfig = null;
			if (dao instanceof BaseDaoImpl) {
				tableConfig = ((BaseDaoImpl<?, ?>) dao).getTableConfig();
			}
			if (tableConfig == null) {
				tableConfig = DatabaseTableConfig.fromClass(connectionSource.getDatabaseType(), clazz);
			}
			TableUtils.createTable(connectionSource, tableConfig);
			createdClasses.add(tableConfig);
		} catch (Exception e) {
			// we don't stop because the table might already exist
			System.err.println("Was unable to auto-create table for " + clazz);
			e.printStackTrace();
		}
	}
}
 
Example 5
Source File: BaseJdbcTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
private <T, ID> Dao<T, ID> configDao(BaseDaoImpl<T, ID> dao, boolean createTable) throws Exception {
	if (connectionSource == null) {
		throw new SQLException(DATASOURCE_ERROR);
	}
	dao.setConnectionSource(connectionSource);
	if (createTable) {
		DatabaseTableConfig<T> tableConfig = dao.getTableConfig();
		if (tableConfig == null) {
			tableConfig = DatabaseTableConfig.fromClass(databaseType, dao.getDataClass());
		}
		createTable(tableConfig, true);
	}
	dao.initialize();
	return dao;
}
 
Example 6
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testTableConfig() throws Exception {
	DatabaseTableConfig<Foo> config = DatabaseTableConfig.fromClass(databaseType, Foo.class);
	BaseDaoImpl<Foo, Integer> dao = new BaseDaoImpl<Foo, Integer>(connectionSource, config) {
	};
	assertSame(config, dao.getTableConfig());
}
 
Example 7
Source File: BaseDaoImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testSetters() throws Exception {
	DatabaseTableConfig<Foo> config = DatabaseTableConfig.fromClass(databaseType, Foo.class);
	BaseDaoImpl<Foo, Integer> dao = new BaseDaoImpl<Foo, Integer>(Foo.class) {
	};
	dao.setTableConfig(config);
	dao.setConnectionSource(connectionSource);
	assertSame(config, dao.getTableConfig());
}
 
Example 8
Source File: DaoManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testClassLoopWithLoadedConfig() throws Exception {
	DaoManager.clearCache();
	DatabaseTableConfig<LoopOne> config1 = DatabaseTableConfig.fromClass(databaseType, LoopOne.class);
	DatabaseTableConfig<LoopTwo> config2 = DatabaseTableConfig.fromClass(databaseType, LoopTwo.class);
	@SuppressWarnings("unchecked")
	List<DatabaseTableConfig<?>> configs = new ArrayList<DatabaseTableConfig<?>>(Arrays.asList(config1, config2));
	DaoManager.addCachedDatabaseConfigs(configs);
	assertNotNull(DaoManager.createDao(connectionSource, LoopOne.class));
	assertNotNull(DaoManager.createDao(connectionSource, LoopTwo.class));
}