Elffuss BraidHub

kikocis/java-driver public

Ignore missing columns in mapper (JAVA-824).
Changesetdfc0c00933c123d891a767a5a5b27cd75172e797d7e969e0ffddbbbd98df1af3
Authorolim7t <omichallat+github@gmail.com> · 2015-06-26 18:15 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-26 18:19 +02:00
Parents1b19082d891d

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -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
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Result.java +3 −2
@@ -44,9 +44,10 @@
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 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +38 −0
@@ -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 @@
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 }