FieldMapFactory { public static <T> FieldMap buildFieldMapForEntity(Class<T> clazz) { if (FIELD_MAP_CACHE.containsKey(clazz)) { return FIELD_MAP_CACHE.get(clazz); } FieldMap fieldMap = new FieldMap(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.isAnnotationPresent(Column.class)) { column(fieldMap, field); } else if (field.isAnnotationPresent(JoinColumn.class)) { manyToOne(fieldMap, field); } else if (field.isAnnotationPresent(ManyToMany.class) && field.isAnnotationPresent(JoinTable.class)) { manyToMany(fieldMap, field); } else { embeddable(fieldMap, field); } } fieldMap.afterFieldsSet(); FIELD_MAP_CACHE.put(clazz, fieldMap); return fieldMap; }  static FieldMap buildFieldMapForEntity(Class<T> clazz);  }
@Test public void should_process_one_to_many_column() throws InstantiationException, IllegalAccessException { FieldMap fieldDefinitionMap = FieldMapFactory.buildFieldMapForEntity(Project.class); FieldDefinition fieldDef = fieldDefinitionMap.get("customer_id"); Customer customer = CustomerObjectMother.createCustomer(); Project project = ProjectObjectMother.createProject(1); project.setCustomer(null); Map<Class<?>, Object> embeddables = Maps.newHashMap(); fieldDef.process(project, embeddables, customer); assertEquals(customer, project.getCustomer()); }
@Test public void should_process_column_with_basic_value() throws InstantiationException, IllegalAccessException { FieldMap fieldDefinitionMap = FieldMapFactory.buildFieldMapForEntity(Project.class); FieldDefinition fieldDef = fieldDefinitionMap.get("project_code"); Project project = ProjectObjectMother.createProject(1); project.setProjectCode(null); Map<Class<?>, Object> embeddables = Maps.newHashMap(); fieldDef.process(project, embeddables, "TEC"); assertEquals("TEC", project.getProjectCode()); }
@Test public void should_process_column_with_embeddable() throws InstantiationException, IllegalAccessException { FieldMap fieldDefinitionMap = FieldMapFactory.buildFieldMapForEntity(TimesheetEntry.class); FieldDefinition fieldDef = fieldDefinitionMap.get("assignment_id"); TimesheetEntry timesheetEntry = TimesheetEntryObjectMother.createTimesheetEntry(1, new Date(), 5f); timesheetEntry.setEntryId(null); Map<Class<?>, Object> embeddables = Maps.newHashMap(); ProjectAssignment projectAssignment = ProjectAssignmentObjectMother.createProjectAssignment(1); fieldDef.process(timesheetEntry, embeddables, projectAssignment); assertEquals(projectAssignment, ((TimesheetEntryId) embeddables.get(TimesheetEntryId.class)).getProjectAssignment()); }