Elffuss BraidHub

kikocis/java-driver public

Enhancements to documentation.
Changesetdd37f80f302211590cc52f4428375102059a15ce67a2f3ec1c29a5cd3ca99c77
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-06-01 14:13 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-07-22 09:47 -07:00
Parentsdd6f2ca5f35e

Browse the files at this changeset

1 file changed

modified manual/object_mapper/creating/README.md +98 −53
@@ -39,11 +39,25 @@
3939 save, or delete operation in the mapper. (if unspecified, it defaults
4040 to the cluster-wide setting)
4141
42-The class must provide a default constructor (it can however be private).
43-
44-The mapping of each table column can be customized through annotations placed on either a field declaration,
45-or on a Java bean property getter method. _Annotations on setters are not supported and will be
46-silently ignored by the mapper_.
42+The class must provide a default constructor. The default constructor
43+is allowed to be non-public, provided that the security manager, if any, grants
44+the mapper access to it via [reflection][set-accessible].
45+
46+#### Mapping table columns
47+
48+The mapping of each table column can be customized through annotations
49+placed on either a field declaration, or on a getter method of a
50+[Java bean property][java-beans]. _Annotations on setters are not
51+supported and will be silently ignored by the mapper_.
52+
53+Users are strongly encouraged to follow the Java bean property
54+conventions when designing mapped classes: if this is the case, the mapper will
55+transparently "bind" together fields and accessor methods (getters and setters)
56+into a single logical abstraction; e.g. a field named `myCol` and a Java
57+bean property named `myCol` offering a public getter named `getMyCol()`
58+will be assumed to represent the very same table column,
59+and annotations for this column are thus allowed to be placed indifferently
60+on the field declaration, or on the getter method declaration.
4761
4862 The following mappings are thus equivalent:
4963
@@ -53,62 +67,97 @@
5367
5468 // annotation on a field
5569 @PartitionKey
56- @Column(name = "user_id")
57- private UUID userId;
70+ private UUID id;
5871
59- public UUID getUserId() {
60- return userId;
72+ public UUID getId() {
73+ return id;
6174 }
6275
63- public void setUserId(UUID userId) {
64- this.userId = userId;
76+ public void setId(UUID id) {
77+ this.id = id;
6578 }
6679
6780 }
81+```
6882
83+```java
6984 @Table(name = "users")
7085 public static class User {
7186
72- private UUID userId;
87+ private UUID id;
7388
7489 // annotation on a getter method
7590 @PartitionKey
76- @Column(name = "user_id")
77- public UUID getUserId() {
78- return userId;
91+ public UUID getId() {
92+ return id;
7993 }
8094
81- public void setUserId(UUID userId) {
82- this.userId = userId;
95+ public void setId(UUID id) {
96+ this.id = id;
8397 }
8498
8599 }
86100
87101 ```
88102
89-To explicitly exclude a Java bean property or a field from being mapped,
90-annotate either the field or the property getter method with `@Transient`.
103+It is recommended to adopt one strategy or the other for
104+placing annotations on class members (i.e., either always on fields, or always on getters),
105+but not both at the same time, unless strictly required.
106+Annotating fields is usually less verbose, but with polymorphic
107+data models (see below), annotating getters generally proves to be more flexible.
108+
109+If duplicated annotations for a given column are found on the field declaration
110+and on the getter method declaration, the one declared in the getter method
111+declaration wins. No warnings are generated in such situations.
112+
113+When reading or writing mapped property values, the mapper will first
114+try to invoke the property getter and setter methods, if they are available;
115+and if they are not, the mapper will try a direct access to the property's corresponding field.
91116
92-The mapper tries to "bind" fields and accessor methods (getters and setters) together
93-into a single logical abstraction;
94-it is therefore advised that users follow the [Java bean property conventions]
95-(java-beans) when designing mapped classes.
117+A mapped field is thus allowed to be non-public if:
96118
97-In particular, when reading or writing mapped property values, the mapper will first
98-try the property getter and setter methods, if they are available;
99-and if they are not, the mapper will try a direct access to the property's corresponding field.
119+1. Reads and writes are done through its public getters and setters; or
120+2. The security manager, if any, grants the mapper access to it via [reflection][set-accessible].
100121
101122 [table]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Table.html
102123 [case-sensitive]:http://docs.datastax.com/en/cql/3.3/cql/cql_reference/ucase-lcase_r.html
103124 [consistency level]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ConsistencyLevel.html
104125 [java-beans]:https://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html
126+[set-accessible]:https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AccessibleObject.html#setAccessible-boolean-
105127
106128 #### Column names
107129
108-By default, the mapper tries to map each Java property to a
109-case insensitive column of the same name. If you want to use a different
110-name, or [case-sensitive] names, use the [@Column][column] annotation on
111-the property.
130+By default, the mapper tries to map each Java bean property to a
131+column of the same name. _The mapping is case-insensitive by default_,
132+i.e., the property `userId` would map to a column named `userid`.
133+
134+If you want to use a different name, or [case-sensitive] names,
135+use the [@Column][column] annotation.
136+
137+To specify a different column name, use the `name` attribute:
138+
139+```
140+CREATE TABLE users(id uuid PRIMARY KEY, user_name text);
141+```
142+
143+```java
144+// column name does not match field name
145+@Column(name = "user_name")
146+private String userName;
147+```
148+
149+When case-sensitive identifiers are required, use the
150+`caseSensitive` attribute:
151+
152+```
153+CREATE TABLE users(id uuid PRIMARY KEY, "userName" text);
154+```
155+
156+```java
157+// column name is case-sensitive
158+@Column(caseSensitive = true)
159+private String userName;
160+```
112161
113162 [column]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Column.html
114163
@@ -175,8 +224,10 @@
175224
176225 #### Transient properties
177226
178-[@Transient][transient] can be used to prevent a field or a Java bean property from being mapped.
179-It should be placed on either the field declaration or the property getter method.
227+By default, the mapper will try to map all fields and Java bean properties
228+to table columns. [@Transient][transient] can be used to prevent a field or
229+a Java bean property from being mapped. Like other column-level annotations,
230+it should be placed on either the field declaration or the property getter method.
180231
181232 [transient]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Transient.html
182233
@@ -205,7 +256,9 @@
205256 - `caseSensitiveKeyspace`: whether the keyspace name is case-sensitive.
206257 - `caseSensitiveType`: whether the UDT name is case-sensitive.
207258
208-The class must provide a default constructor (it can however be private).
259+The class must provide a default constructor. The default constructor
260+is allowed to be non-public, provided that the security manager, if any, grants
261+the mapper access to it via [reflection][set-accessible].
209262
210263 As for table entities, properties in UDT classes are mapped to an UDT field of the same
211264 name by default.
@@ -283,17 +336,15 @@
283336
284337 ### Polymorphism support
285338
286-By default, the mapper will consider all Java bean properties and all fields
287-(including private ones) found in an entity class or in a UDT class,
288-as well as in their superclasses and superinterfaces.
289-
290-This allows for a (basic) polymorphic mapping of a given class hierarchy
339+When mapping an entity class or a UDT class, the mapper will transparently
340+scan superclasses and superinterfaces for annotations on fields and getter methods,
341+thus enabling the polymorphic mapping of one class hierarchy
291342 into different CQL tables or UDTs.
292343
293-Currently, each concrete class must correspond to a table or a UDT,
294-and should be annotated accordingly. This corresponds to the so called
295-"Table per concrete class" mapping strategy provided by
296-some popular SQL mapping frameworks such as Hibernate.
344+Each concrete class must correspond to a table or a UDT,
345+and should be annotated accordingly with `@Table` or `@UDT`.
346+This is analogous to the so-called "table per concrete class"
347+mapping strategy usually found in popular SQL mapping frameworks, such as Hibernate.
297348
298349 Here is an example of a polymorphic mapping:
299350
@@ -419,14 +470,8 @@
419470 }
420471 ```
421472
422-It is usually recommended to adopt one strategy or the other for
423-placing annotations on class members (i.e., either always on fields, or always on getters),
424-but not both at the same time, unless strictly required.
425-Usually, annotating getters is more flexible than annotating fields.
426-
427-If the mapper needs to disambiguate a specific property mapping,
428-note that annotations on getters will always take precedence over annotations on fields.
429-Likewise, if a getter method is overridden in a subclass, annotations in both
430-methods will get merged together, and in case of conflict, the overriding method's annotations
431-will take precedence over the overridden's.
432-
473+One powerful advantage of annotating getter methods is that
474+annotations are inherited from overridden methods in superclasses and superinterfaces;
475+in other words, if a getter method is overridden in a subclass, annotations in both
476+method declarations will get merged together. If duplicated annotations are found during this merge
477+process, the overriding method's annotations will take precedence over the overridden's.