Java Code Examples for com.j256.ormlite.field.DatabaseFieldConfig#setColumnName()

The following examples show how to use com.j256.ormlite.field.DatabaseFieldConfig#setColumnName() . 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: DatabaseTableConfigLoaderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testConfigFile() throws Exception {
	DatabaseTableConfig<NoFields> config = new DatabaseTableConfig<NoFields>();
	StringBuilder body = new StringBuilder();
	StringWriter writer = new StringWriter();
	BufferedWriter buffer = new BufferedWriter(writer);

	Class<NoFields> clazz = NoFields.class;
	config.setDataClass(clazz);
	body.append("dataClass=").append(clazz.getName()).append(LINE_SEP);
	checkConfigOutput(config, body, writer, buffer, false);

	String tableName = "pojgefwpjoefwpjo";
	config.setTableName(tableName);
	body.append("tableName=").append(tableName).append(LINE_SEP);
	checkConfigOutput(config, body, writer, buffer, false);

	DatabaseFieldConfig field1 = new DatabaseFieldConfig();
	String columnName = "efjpowefpjoefw";
	field1.setColumnName(columnName);
	config.setFieldConfigs(Arrays.asList(field1));
	StringWriter fieldWriter = new StringWriter();
	BufferedWriter fieldBuffer = new BufferedWriter(fieldWriter);
	DatabaseFieldConfigLoader.write(fieldBuffer, field1, tableName);
	fieldBuffer.flush();
	body.append("# --table-fields-start--").append(LINE_SEP);
	body.append(fieldWriter.toString());
	checkConfigOutput(config, body, writer, buffer, true);
}
 
Example 2
Source File: JdbcBaseDaoImplTest.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
@Test
public void testFieldConfigForeign() throws Exception {
	List<DatabaseFieldConfig> noAnnotationsFieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig field1 = new DatabaseFieldConfig("id");
	field1.setColumnName("idthingie");
	field1.setGeneratedId(true);
	noAnnotationsFieldConfigs.add(field1);
	DatabaseFieldConfig field2 = new DatabaseFieldConfig("stuff");
	field2.setColumnName("stuffy");
	noAnnotationsFieldConfigs.add(field2);
	DatabaseTableConfig<NoAnno> noAnnotationsTableConfig =
			new DatabaseTableConfig<NoAnno>(databaseType, NoAnno.class, noAnnotationsFieldConfigs);
	Dao<NoAnno, Integer> noAnnotationDao = createDao(noAnnotationsTableConfig, true);
	NoAnno noa = new NoAnno();
	String stuff = "qpoqwpjoqwp12";
	noa.stuff = stuff;
	assertEquals(1, noAnnotationDao.create(noa));
	assertNotNull(noAnnotationDao.queryForId(noa.id));

	List<DatabaseFieldConfig> noAnnotationsForiegnFieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig field3 = new DatabaseFieldConfig("id");
	field3.setColumnName("anotherid");
	field3.setGeneratedId(true);
	noAnnotationsForiegnFieldConfigs.add(field3);
	DatabaseFieldConfig field4 = new DatabaseFieldConfig("foreign");
	field4.setColumnName("foreignThingie");
	field4.setForeign(true);
	field4.setForeignTableConfig(noAnnotationsTableConfig);
	noAnnotationsForiegnFieldConfigs.add(field4);
	DatabaseTableConfig<NoAnnoFor> noAnnotationsForiegnTableConfig =
			new DatabaseTableConfig<NoAnnoFor>(databaseType, NoAnnoFor.class, noAnnotationsForiegnFieldConfigs);

	Dao<NoAnnoFor, Integer> noAnnotaionForeignDao = createDao(noAnnotationsForiegnTableConfig, true);
	NoAnnoFor noaf = new NoAnnoFor();
	noaf.foreign = noa;
	assertEquals(1, noAnnotaionForeignDao.create(noaf));

	NoAnnoFor noaf2 = noAnnotaionForeignDao.queryForId(noaf.id);
	assertNotNull(noaf2);
	assertEquals(noaf.id, noaf2.id);
	assertEquals(noa.id, noaf2.foreign.id);
	assertNull(noaf2.foreign.stuff);
	assertEquals(1, noAnnotationDao.refresh(noaf2.foreign));
	assertEquals(stuff, noaf2.foreign.stuff);
}
 
Example 3
Source File: JavaxPersistenceImpl.java    From ormlite-core with ISC License 4 votes vote down vote up
@Override
public DatabaseFieldConfig createFieldConfig(DatabaseType databaseType, Field field) {
	Column columnAnnotation = field.getAnnotation(Column.class);
	Basic basicAnnotation = field.getAnnotation(Basic.class);
	Id idAnnotation = field.getAnnotation(Id.class);
	GeneratedValue generatedValueAnnotation = field.getAnnotation(GeneratedValue.class);
	OneToOne oneToOneAnnotation = field.getAnnotation(OneToOne.class);
	ManyToOne manyToOneAnnotation = field.getAnnotation(ManyToOne.class);
	JoinColumn joinColumnAnnotation = field.getAnnotation(JoinColumn.class);
	Enumerated enumeratedAnnotation = field.getAnnotation(Enumerated.class);
	Version versionAnnotation = field.getAnnotation(Version.class);

	if (columnAnnotation == null && basicAnnotation == null && idAnnotation == null && oneToOneAnnotation == null
			&& manyToOneAnnotation == null && enumeratedAnnotation == null && versionAnnotation == null) {
		return null;
	}

	DatabaseFieldConfig config = new DatabaseFieldConfig();
	String fieldName = field.getName();
	if (databaseType.isEntityNamesMustBeUpCase()) {
		fieldName = databaseType.upCaseEntityName(fieldName);
	}
	config.setFieldName(fieldName);

	if (columnAnnotation != null) {
		if (stringNotEmpty(columnAnnotation.name())) {
			config.setColumnName(columnAnnotation.name());
		}
		if (stringNotEmpty(columnAnnotation.columnDefinition())) {
			config.setColumnDefinition(columnAnnotation.columnDefinition());
		}
		config.setWidth(columnAnnotation.length());
		config.setCanBeNull(columnAnnotation.nullable());
		config.setUnique(columnAnnotation.unique());
	}
	if (basicAnnotation != null) {
		config.setCanBeNull(basicAnnotation.optional());
	}
	if (idAnnotation != null) {
		if (generatedValueAnnotation == null) {
			config.setId(true);
		} else {
			// generatedValue only works if it is also an id according to {@link GeneratedValue)
			config.setGeneratedId(true);
		}
	}
	if (oneToOneAnnotation != null || manyToOneAnnotation != null) {
		// if we have a collection then make it a foreign collection
		if (Collection.class.isAssignableFrom(field.getType())
				|| ForeignCollection.class.isAssignableFrom(field.getType())) {
			config.setForeignCollection(true);
			if (joinColumnAnnotation != null && stringNotEmpty(joinColumnAnnotation.name())) {
				config.setForeignCollectionColumnName(joinColumnAnnotation.name());
			}
			if (manyToOneAnnotation != null) {
				FetchType fetchType = manyToOneAnnotation.fetch();
				if (fetchType != null && fetchType == FetchType.EAGER) {
					config.setForeignCollectionEager(true);
				}
			}
		} else {
			// otherwise it is a foreign field
			config.setForeign(true);
			if (joinColumnAnnotation != null) {
				if (stringNotEmpty(joinColumnAnnotation.name())) {
					config.setColumnName(joinColumnAnnotation.name());
				}
				config.setCanBeNull(joinColumnAnnotation.nullable());
				config.setUnique(joinColumnAnnotation.unique());
			}
		}
	}
	if (enumeratedAnnotation != null) {
		EnumType enumType = enumeratedAnnotation.value();
		if (enumType != null && enumType == EnumType.STRING) {
			config.setDataType(DataType.ENUM_STRING);
		} else {
			config.setDataType(DataType.ENUM_INTEGER);
		}
	}
	if (versionAnnotation != null) {
		// just the presence of the version...
		config.setVersion(true);
	}
	if (config.getDataPersister() == null) {
		config.setDataPersister(DataPersisterManager.lookupForField(field));
	}
	config.setUseGetSet(DatabaseFieldConfig.findGetMethod(field, databaseType, false) != null
			&& DatabaseFieldConfig.findSetMethod(field, databaseType, false) != null);
	return config;
}