55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * https://www.apache.org/licenses/LICENSE-2.0
8+ * https://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
2121import java .util .ArrayList ;
2222import java .util .List ;
2323import java .util .Objects ;
24+ import org .springframework .data .annotation .Version ;
2425
2526/** Example POJO to demonstrate Spring Cloud GCP Spring Data Firestore operations. */
2627@ Document (collectionName = "users" )
@@ -32,6 +33,13 @@ public class User {
3233
3334 List <Pet > pets ;
3435
36+ /**
37+ * The version field enables optimistic locking.
38+ * Spring Data increments this value automatically on every update.
39+ */
40+ @ Version
41+ Long version ;
42+
3543 User () {
3644 pets = new ArrayList <>();
3745 }
@@ -66,6 +74,14 @@ public void setPets(List<Pet> pets) {
6674 this .pets = pets ;
6775 }
6876
77+ public Long getVersion () {
78+ return version ;
79+ }
80+
81+ public void setVersion (Long version ) {
82+ this .version = version ;
83+ }
84+
6985 @ Override
7086 public boolean equals (Object o ) {
7187 if (this == o ) {
@@ -75,16 +91,24 @@ public boolean equals(Object o) {
7591 return false ;
7692 }
7793 User user = (User ) o ;
78- return age == user .age && Objects .equals (name , user .name ) && Objects .equals (pets , user .pets );
94+ return age == user .age
95+ && Objects .equals (name , user .name )
96+ && Objects .equals (pets , user .pets )
97+ && Objects .equals (version , user .version );
7998 }
8099
81100 @ Override
82101 public int hashCode () {
83- return Objects .hash (name , age , pets );
102+ return Objects .hash (name , age , pets , version );
84103 }
85104
86105 @ Override
87106 public String toString () {
88- return "User{" + "name='" + name + '\'' + ", age=" + age + ", pets=" + pets + '}' ;
107+ return "User{"
108+ + "name='" + name + '\''
109+ + ", age=" + age
110+ + ", pets=" + pets
111+ + ", version=" + version
112+ + '}' ;
89113 }
90- }
114+ }
0 commit comments