#2015 - Dto instance id fields are not populated when queried using DtoQuery (and no select clause is specified)

This commit is contained in:
rob bygrave
2020-05-27 22:05:39 +12:00
parent f97359502b
commit cbdf5609b2
5 changed files with 82 additions and 7 deletions
@@ -788,7 +788,7 @@ public interface SpiQuery<T> extends Query<T>, TxnProfileEventCodes {
/**
* Set to true when we only include the Id property if it is explicitly included in the select().
*/
void setManualId(boolean manualId);
void setManualId();
/**
* Set default select clauses where none have been explicitly defined.
@@ -47,7 +47,7 @@ public final class DtoQueryRequest<T> extends AbstractSqlQueryRequest {
SpiQuery<?> ormQuery = query.getOrmQuery();
if (ormQuery != null) {
ormQuery.setType(type);
ormQuery.setManualId(true);
ormQuery.setManualId();
// execute the underlying ORM query returning the ResultSet
SpiResultSet result = server.findResultSet(ormQuery, trans);
@@ -1715,8 +1715,10 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
@Override
public void setManualId(boolean manualId) {
this.manualId = manualId;
public void setManualId() {
if (detail != null && detail.hasSelectClause()) {
this.manualId = true;
}
}
/**
@@ -140,6 +140,32 @@ public class DtoQueryFromOrmTest extends BaseTestCase {
+ " fullName from contact t0 where t0.email is not null and t0.last_name is not null order by t0.last_name");
}
@Test
public void asDto_withoutSelectClause() {
ResetBasicData.reset();
LoggedSqlCollector.start();
DtoQuery<ContactDto2> query = DB.find(Contact.class)
.where().isNotNull("email").order().asc("lastName")
.asDto(ContactDto2.class)
.setRelaxedMode();
List<ContactDto2> dtos = query.findList();
assertThat(dtos).isNotEmpty();
for (ContactDto2 dto : dtos) {
assertThat(dto.getEmail()).isNotNull();
assertThat(dto.getFirstName()).isNotNull();
assertThat(dto.getLastName()).isNotNull();
assertThat(dto.getId()).isGreaterThan(0);
}
List<String> sql = LoggedSqlCollector.stop();
assertSql(sql.get(0)).contains("select t0.id, t0.first_name, t0.last_name");
}
@Test
public void asDto_withoutExplicitId() {
@@ -326,6 +352,46 @@ public class DtoQueryFromOrmTest extends BaseTestCase {
}
}
public static class ContactDto2 {
int id;
String firstName;
String lastName;
String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public static class ContactDto {
String email;
@@ -308,12 +308,19 @@ public class OrmQueryPlanKeyTest extends BaseExpressionTest {
}
@Test
public void equals_when_manualId() {
public void equals_when_manualId_andSelectClause() {
DefaultOrmQuery<Customer> q1 = query().select("name");
q1.setManualId();
assertDifferent(q1, query().select("name"));
}
@Test
public void equals_when_manualId_andNoSelectClause() {
DefaultOrmQuery<Customer> q1 = query();
q1.setManualId(true);
q1.setManualId();
assertDifferent(q1, query());
assertSame(q1, query());
}
private CQueryPlanKey planKey(ExpressionList<Customer> id) {