mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Merge pull request #1597 from elser/i1594-LinkedHashSet-in-DefaultMapper
I1594 linked hash set in default mapper
This commit is contained in:
commit
710d89a833
@ -29,7 +29,7 @@ import com.querydsl.sql.RelationalPath;
|
||||
public abstract class AbstractMapper<T> implements Mapper<T> {
|
||||
|
||||
protected Map<String, Path<?>> getColumns(RelationalPath<?> path) {
|
||||
Map<String, Path<?>> columns = Maps.newHashMap();
|
||||
Map<String, Path<?>> columns = Maps.newLinkedHashMap();
|
||||
for (Path<?> column : path.getColumns()) {
|
||||
columns.put(column.getMetadata().getName(), column);
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ package com.querydsl.sql.dml;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.querydsl.core.QueryException;
|
||||
@ -51,7 +51,7 @@ public class DefaultMapper extends AbstractMapper<Object> {
|
||||
@Override
|
||||
public Map<Path<?>, Object> createMap(RelationalPath<?> entity, Object bean) {
|
||||
try {
|
||||
Map<Path<?>, Object> values = new HashMap<Path<?>, Object>();
|
||||
Map<Path<?>, Object> values = new LinkedHashMap<Path<?>, Object>();
|
||||
Class<?> beanClass = bean.getClass();
|
||||
Map<String, Path<?>> columns = getColumns(entity);
|
||||
for (Field beanField : ReflectionUtils.getFields(beanClass)) {
|
||||
|
||||
@ -3,7 +3,7 @@ package com.querydsl.sql.dml;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@ -31,4 +31,12 @@ public class DefaultMapperTest extends AbstractMapperTest {
|
||||
assertTrue(values.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void PreservedColumnOrder() {
|
||||
final Map<String, Path<?>> columns = DefaultMapper.DEFAULT.getColumns(emp);
|
||||
final List<String> expectedKeySet = Arrays.asList("id", "firstname", "lastname",
|
||||
"salary", "datefield", "timefield", "superiorId");
|
||||
assertEquals(expectedKeySet, new ArrayList<String>(columns.keySet()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -22,4 +22,17 @@ public class SQLInsertClauseTest {
|
||||
assertEquals(ImmutableList.of(1), sql.getBindings());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void GetSQLWithPreservedColumnOrder() {
|
||||
com.querydsl.sql.domain.QEmployee emp1 = new com.querydsl.sql.domain.QEmployee("emp1");
|
||||
SQLInsertClause insert = new SQLInsertClause(null, SQLTemplates.DEFAULT, emp1);
|
||||
insert.populate(emp1);
|
||||
|
||||
SQLBindings sql = insert.getSQL().get(0);
|
||||
assertEquals("The order of columns in generated sql should be predictable",
|
||||
"insert into EMPLOYEE (SALARY, SUPERIOR_ID, DATEFIELD, FIRSTNAME, TIMEFIELD, ID, LASTNAME)\n" +
|
||||
"values (EMPLOYEE.SALARY, EMPLOYEE.SUPERIOR_ID, EMPLOYEE.DATEFIELD, EMPLOYEE.FIRSTNAME, " +
|
||||
"EMPLOYEE.TIMEFIELD, EMPLOYEE.ID, EMPLOYEE.LASTNAME)", sql.getSQL());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user