Skip to content

Commit 77d5e9b

Browse files
committed
Ignore missing columns in mapper (JAVA-824).
1 parent 5a8b3f7 commit 77d5e9b

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [improvement] Allow @Enumerated in Accessor method parameters (JAVA-589)
1919
- [improvement] Allow access to table metadata from Mapper (JAVA-554)
2020
- [improvement] Provide a way to map computed fields (JAVA-661)
21+
- [improvement] Ignore missing columns in mapper (JAVA-824)
2122

2223

2324
### 2.1.6

driver-mapping/src/main/java/com/datastax/driver/mapping/Result.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public class Result<T> implements Iterable<T> {
4444
private T map(Row row) {
4545
T entity = mapper.newEntity();
4646
for (ColumnMapper<T> cm : mapper.allColumns()) {
47-
if (!useAlias && cm.kind == ColumnMapper.Kind.COMPUTED)
47+
String name = cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName();
48+
if (!row.getColumnDefinitions().contains(name))
4849
continue;
49-
ByteBuffer bytes = row.getBytesUnsafe(cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName());
50+
ByteBuffer bytes = row.getBytesUnsafe(name);
5051
if (bytes != null)
5152
cm.setValue(entity, cm.getDataType().deserialize(bytes, protocolVersion));
5253
}

driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.google.common.base.Objects;
2323
import com.google.common.util.concurrent.ListenableFuture;
2424
import org.testng.annotations.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
2527
import static org.testng.Assert.*;
2628

2729
import com.datastax.driver.mapping.MapperTest.User.Gender;
@@ -377,4 +379,40 @@ public void testDynamicEntity() throws Exception {
377379
userAccessor.updateNameAndGender("Paule", null, u1.getUserId());
378380
assertEquals(userMapper.get(u1.getUserId()).getGender(), null);
379381
}
382+
383+
@Test(groups="short")
384+
public void should_map_objects_from_partial_queries() throws Exception {
385+
MappingManager manager = new MappingManager(session);
386+
387+
Mapper<Post> m = manager.mapper(Post.class);
388+
389+
// Insert a few posts
390+
User u1 = new User("Paul", "paul@gmail.com", User.Gender.MALE);
391+
Post p1 = new Post(u1, "Something about mapping");
392+
Post p2 = new Post(u1, "Something else");
393+
Post p3 = new Post(u1, "Something more");
394+
395+
p1.setDevice(InetAddress.getLocalHost());
396+
p2.setTags(new HashSet<String>(Arrays.asList("important", "keeper")));
397+
398+
m.save(p1);
399+
m.save(p2);
400+
m.save(p3);
401+
402+
// Retrieve posts with a projection query that only retrieves some of the fields
403+
ResultSet rs = session.execute("select user_id, post_id, title from posts where user_id = ?", u1.getUserId());
404+
405+
Result<Post> result = m.map(rs);
406+
for (Post post : result) {
407+
assertThat(post.getUserId()).isEqualTo(u1.getUserId());
408+
assertThat(post.getPostId()).isNotNull();
409+
assertThat(post.getTitle()).isNotNull();
410+
411+
assertThat(post.getDevice()).isNull();
412+
assertThat(post.getTags()).isNull();
413+
}
414+
415+
// cleanup
416+
session.execute("delete from posts where user_id = ?", u1.getUserId());
417+
}
380418
}

0 commit comments

Comments
 (0)