Elffuss BraidHub

kikocis/java-driver public

JAVA-1255: Allow nested UDTs to be used in Mapper (#719)

Changesetbcd09728a1db952fb8991076eff55f0681b39242cbd0e49497ddbdf20a905951
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-10-03 12:01 -05:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-10-03 10:01 -07:00
Parentsd49a67f537ce

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -3,6 +3,7 @@
33 ### 3.1.1
44
55 - [bug] JAVA-1284: ClockFactory should check system property before attempting to load Native class.
6+- [bug] JAVA-1255: Allow nested UDTs to be used in Mapper.
67
78 Merged from 3.0.x:
89
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +3 −0
@@ -201,6 +201,9 @@
201201 throw new IllegalArgumentException(String.format("Field %s does not exist in type %s.%s",
202202 propertyMapper.columnName, ksName, userType.getTypeName()));
203203
204+ for (Class<?> fieldUdt : TypeMappings.findUDTs(propertyMapper.javaType.getType()))
205+ mappingManager.getUDTCodec(fieldUdt);
206+
204207 propertyMappers.put(propertyMapper.columnName, propertyMapper);
205208 }
206209
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperNestedUDTTest.java +271 −0
@@ -0,0 +1,271 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.mapping;
17+
18+import com.datastax.driver.core.CCMTestsSupport;
19+import com.datastax.driver.core.utils.CassandraVersion;
20+import com.datastax.driver.mapping.annotations.PartitionKey;
21+import com.datastax.driver.mapping.annotations.Table;
22+import com.datastax.driver.mapping.annotations.Transient;
23+import com.datastax.driver.mapping.annotations.UDT;
24+import org.testng.annotations.Test;
25+
26+import java.util.Arrays;
27+import java.util.UUID;
28+
29+import static org.assertj.core.api.Assertions.assertThat;
30+
31+@SuppressWarnings({"unused"})
32+@CassandraVersion(major = 2.1)
33+public class MapperNestedUDTTest extends CCMTestsSupport {
34+
35+ @Override
36+ public void onTestContextInitialized() {
37+ execute(
38+ "CREATE TYPE point (x int, y int)",
39+ "CREATE TYPE rectangle (a frozen<point>, b frozen<point>, c frozen<point>, d frozen<point>)",
40+ "CREATE TABLE user (id uuid PRIMARY KEY, name text, area frozen<rectangle>)"
41+ );
42+ }
43+
44+ /**
45+ * Validates that tables having a UDT column that itself has a UDT field can be handled by the object mapper.
46+ *
47+ * @jira_ticket JAVA-1255
48+ * @test_category object_mapper
49+ */
50+ @Test(groups = "short")
51+ public void should_be_able_to_create_entity_from_table_having_udt_with_nested_udt() {
52+ MappingManager manager = new MappingManager(session());
53+ Mapper<User> mapper = manager.mapper(User.class);
54+
55+ Rectangle r = new Rectangle(
56+ new Point(10, 0),
57+ new Point(10, 10),
58+ new Point(0, 10),
59+ new Point(0, 0)
60+ );
61+
62+ User user = new User();
63+ user.setId(UUID.randomUUID());
64+ user.setName("Bob");
65+ user.setArea(r);
66+
67+ mapper.save(user);
68+
69+ User retrieved = mapper.get(user.getId());
70+ assertThat(retrieved).isEqualTo(user);
71+ }
72+
73+ @Table(name = "user")
74+ public static class User {
75+ @PartitionKey
76+ private UUID id;
77+
78+ private String name;
79+
80+ private Rectangle area;
81+
82+ public UUID getId() {
83+ return id;
84+ }
85+
86+ public void setId(UUID id) {
87+ this.id = id;
88+ }
89+
90+ public String getName() {
91+ return name;
92+ }
93+
94+ public void setName(String name) {
95+ this.name = name;
96+ }
97+
98+ public Rectangle getArea() {
99+ return area;
100+ }
101+
102+ public void setArea(Rectangle area) {
103+ this.area = area;
104+ }
105+
106+ @Override
107+ public String toString() {
108+ return "User{" +
109+ "id=" + id +
110+ ", name='" + name + '\'' +
111+ ", area=" + area +
112+ '}';
113+ }
114+
115+ @Override
116+ public boolean equals(Object o) {
117+ if (this == o) return true;
118+ if (!(o instanceof User)) return false;
119+
120+ User user = (User) o;
121+
122+ if (id != null ? !id.equals(user.id) : user.id != null) return false;
123+ if (name != null ? !name.equals(user.name) : user.name != null) return false;
124+ return area != null ? area.equals(user.area) : user.area == null;
125+
126+ }
127+
128+ @Override
129+ public int hashCode() {
130+ int result = id != null ? id.hashCode() : 0;
131+ result = 31 * result + (name != null ? name.hashCode() : 0);
132+ result = 31 * result + (area != null ? area.hashCode() : 0);
133+ return result;
134+ }
135+ }
136+
137+
138+ @UDT(name = "rectangle")
139+ public static class Rectangle {
140+
141+ // a somewhat artificial way to store coordinates,
142+ // but the goal is to test that nested UDTs can be discovered through
143+ // getters instead of fields
144+ @Transient
145+ private final Point[] points;
146+
147+ public Rectangle() {
148+ points = new Point[4];
149+ }
150+
151+ public Rectangle(Point a, Point b, Point c, Point d) {
152+ points = new Point[]{a, b, c, d};
153+ }
154+
155+ public Point getA() {
156+ return points[0];
157+ }
158+
159+ public void setA(Point a) {
160+ points[0] = a;
161+ }
162+
163+ public Point getB() {
164+ return points[1];
165+ }
166+
167+ public void setB(Point b) {
168+ points[1] = b;
169+ }
170+
171+ public Point getC() {
172+ return points[2];
173+ }
174+
175+ public void setC(Point c) {
176+ points[2] = c;
177+ }
178+
179+ public Point getD() {
180+ return points[3];
181+ }
182+
183+ public void setD(Point d) {
184+ points[3] = d;
185+ }
186+
187+ @Override
188+ public String toString() {
189+ return "Rectangle{" +
190+ "a=" + getA() +
191+ ", b=" + getB() +
192+ ", c=" + getC() +
193+ ", d=" + getD() +
194+ '}';
195+ }
196+
197+ @Override
198+ public boolean equals(Object o) {
199+ if (this == o) return true;
200+ if (o == null || getClass() != o.getClass()) return false;
201+ Rectangle rectangle = (Rectangle) o;
202+ return Arrays.equals(points, rectangle.points);
203+ }
204+
205+ @Override
206+ public int hashCode() {
207+ return Arrays.hashCode(points);
208+ }
209+ }
210+
211+
212+ @UDT(name = "point")
213+ public static class Point {
214+ private int x;
215+ private int y;
216+
217+ public Point(int x, int y) {
218+ this.x = x;
219+ this.y = y;
220+ }
221+
222+ public Point() {
223+
224+ }
225+
226+ public int getX() {
227+ return x;
228+ }
229+
230+ public void setX(int x) {
231+ this.x = x;
232+ }
233+
234+ public int getY() {
235+ return y;
236+ }
237+
238+ public void setY(int y) {
239+ this.y = y;
240+ }
241+
242+ @Override
243+ public String toString() {
244+ return "Point{" +
245+ "x=" + x +
246+ ", y=" + y +
247+ '}';
248+ }
249+
250+ @Override
251+ public boolean equals(Object o) {
252+ if (this == o) return true;
253+ if (!(o instanceof Point)) return false;
254+
255+ Point point = (Point) o;
256+
257+ if (x != point.x) return false;
258+ return y == point.y;
259+
260+ }
261+
262+ @Override
263+ public int hashCode() {
264+ int result = x;
265+ result = 31 * result + y;
266+ return result;
267+ }
268+ }
269+
270+
271+}