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

The following examples show how to use com.j256.ormlite.field.DatabaseFieldConfig#setId() . 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: DatabaseTableConfigTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testSetTableNameCase() {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig fieldId = new DatabaseFieldConfig("id");
	fieldId.setId(true);
	fieldConfigs.add(fieldId);
	fieldConfigs.add(new DatabaseFieldConfig("stuff"));

	DatabaseTableConfig<SubWithoutAnno> tableConfig = new DatabaseTableConfig<SubWithoutAnno>();
	tableConfig.setDataClass(SubWithoutAnno.class);
	String tableName = "mixEDcaSE";
	tableConfig.setTableName(tableName);
	tableConfig.setFieldConfigs(fieldConfigs);
	tableConfig.initialize();

	assertEquals(tableName, tableConfig.getTableName());
}
 
Example 2
Source File: DatabaseTableConfigTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testBaseClassHandlingWithoutAnno() throws Exception {
	List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
	DatabaseFieldConfig fieldId = new DatabaseFieldConfig("id");
	fieldId.setId(true);
	fieldConfigs.add(fieldId);
	fieldConfigs.add(new DatabaseFieldConfig("stuff"));

	DatabaseTableConfig<SubWithoutAnno> dbTableConf =
			new DatabaseTableConfig<SubWithoutAnno>(databaseType, SubWithoutAnno.class, fieldConfigs);
	dbTableConf.extractFieldTypes(databaseType);

	FieldType[] fieldTypes = dbTableConf.getFieldTypes(databaseType);
	assertTrue(fieldTypes.length >= 2);
	boolean seeId = false;
	boolean seeStuff = false;
	for (FieldType fieldType : fieldTypes) {
		String fieldName = fieldType.getFieldName();
		if (fieldName.equals("id")) {
			seeId = true;
		} else if (fieldType.getFieldName().equals("stuff")) {
			seeStuff = true;
		}
	}
	assertTrue(seeId);
	assertTrue(seeStuff);
}
 
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;
}