Skip to content

Commit e4c3d39

Browse files
committed
UUID AND Distributed
1 parent 614a99e commit e4c3d39

File tree

5 files changed

+679
-459
lines changed

5 files changed

+679
-459
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.springframework.data.mybatis.domains;
2+
3+
import static org.springframework.data.mybatis.annotations.Id.GenerationType.ASSIGNATION;
4+
5+
import org.springframework.data.domain.Persistable;
6+
import org.springframework.data.mybatis.annotations.Id;
7+
8+
public class StringId implements Persistable<String> {
9+
10+
@Id(strategy = ASSIGNATION)
11+
protected String id;
12+
13+
@Override
14+
public String getId() {
15+
return id;
16+
}
17+
18+
@Override
19+
public boolean isNew() {
20+
return null == id || id.length() == 0;
21+
}
22+
}

src/main/java/org/springframework/data/mybatis/repository/support/MybatisEntityInformation.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,36 @@
1818

1919
package org.springframework.data.mybatis.repository.support;
2020

21+
import java.io.Serializable;
22+
import org.springframework.data.mybatis.annotations.Id.GenerationType;
2123
import org.springframework.data.mybatis.repository.query.MybatisEntityMetadata;
2224
import org.springframework.data.repository.core.EntityInformation;
2325

24-
import java.io.Serializable;
25-
2626
/**
2727
* mybatis entity information.
2828
*
2929
* @author Jarvis Song
3030
*/
3131
public interface MybatisEntityInformation<T, ID extends Serializable>
32-
extends EntityInformation<T, ID>, MybatisEntityMetadata<T> {
32+
extends EntityInformation<T, ID>, MybatisEntityMetadata<T> {
33+
3334

35+
int increaseVersion(T entity);
3436

35-
int increaseVersion(T entity);
37+
void setVersion(T entity, int version);
3638

37-
void setVersion(T entity, int version);
39+
boolean hasVersion();
3840

39-
boolean hasVersion();
41+
GenerationType getIdGenerationType();
4042

43+
void setIdValue(T entity, ID id);
4144

42-
void setCreatedDate(T entity);
45+
void setCreatedDate(T entity);
4346

44-
void setLastModifiedDate(T entity);
47+
void setLastModifiedDate(T entity);
4548

46-
void setCreatedBy(T entity);
49+
void setCreatedBy(T entity);
4750

48-
void setLastModifiedBy(T entity);
51+
void setLastModifiedBy(T entity);
4952

5053
}

src/main/java/org/springframework/data/mybatis/repository/support/MybatisMetamodelEntityInformation.java

Lines changed: 151 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -18,164 +18,202 @@
1818

1919
package org.springframework.data.mybatis.repository.support;
2020

21+
import java.io.Serializable;
22+
import java.lang.annotation.Annotation;
23+
import java.util.Date;
2124
import org.springframework.data.annotation.CreatedBy;
2225
import org.springframework.data.annotation.CreatedDate;
2326
import org.springframework.data.annotation.LastModifiedBy;
2427
import org.springframework.data.annotation.LastModifiedDate;
2528
import org.springframework.data.domain.AuditorAware;
29+
import org.springframework.data.mapping.IdentifierAccessor;
2630
import org.springframework.data.mapping.PersistentEntity;
2731
import org.springframework.data.mapping.PersistentProperty;
2832
import org.springframework.data.mapping.PersistentPropertyAccessor;
33+
import org.springframework.data.mybatis.annotations.Id;
34+
import org.springframework.data.mybatis.annotations.Id.GenerationType;
2935
import org.springframework.data.mybatis.domains.AuditDateAware;
3036
import org.springframework.data.repository.core.support.AbstractEntityInformation;
3137

32-
import java.io.Serializable;
33-
import java.lang.annotation.Annotation;
34-
import java.util.Date;
35-
3638
/**
3739
* mybatis meta.
3840
*
3941
* @author Jarvis Song
4042
*/
41-
public class MybatisMetamodelEntityInformation<T, ID extends Serializable> extends MybatisEntityInformationSupport<T, ID> {
43+
public class MybatisMetamodelEntityInformation<T, ID extends Serializable> extends
44+
MybatisEntityInformationSupport<T, ID> {
45+
46+
private PersistentProperty<?> createdDateProperty;
47+
private PersistentProperty<?> lastModifiedDateProperty;
48+
private PersistentProperty<?> createdByProperty;
49+
private PersistentProperty<?> lastModifiedByProperty;
50+
51+
52+
/**
53+
* Creates a new {@link AbstractEntityInformation} from the given domain class.
54+
*
55+
* @param domainClass must not be {@literal null}.
56+
*/
57+
protected MybatisMetamodelEntityInformation(PersistentEntity<T, ?> persistentEntity,
58+
AuditorAware<?> auditorAware, AuditDateAware<?> auditDateAware, Class<T> domainClass) {
59+
super(persistentEntity, auditorAware, auditDateAware, domainClass);
60+
61+
createdDateProperty = persistentEntity.getPersistentProperty(CreatedDate.class);
62+
lastModifiedDateProperty = persistentEntity.getPersistentProperty(LastModifiedDate.class);
63+
createdByProperty = persistentEntity.getPersistentProperty(CreatedBy.class);
64+
lastModifiedByProperty = persistentEntity.getPersistentProperty(LastModifiedBy.class);
65+
}
66+
67+
@Override
68+
public ID getId(T entity) {
69+
if (null == persistentEntity) {
70+
return null;
71+
}
4272

43-
private PersistentProperty<?> createdDateProperty;
44-
private PersistentProperty<?> lastModifiedDateProperty;
45-
private PersistentProperty<?> createdByProperty;
46-
private PersistentProperty<?> lastModifiedByProperty;
73+
return (ID) persistentEntity.getIdentifierAccessor(entity).getIdentifier();
4774

75+
}
4876

49-
/**
50-
* Creates a new {@link AbstractEntityInformation} from the given domain class.
51-
*
52-
* @param persistentEntity
53-
* @param domainClass must not be {@literal null}.
54-
*/
55-
protected MybatisMetamodelEntityInformation(PersistentEntity<T, ?> persistentEntity, AuditorAware<?> auditorAware, AuditDateAware<?> auditDateAware, Class<T> domainClass) {
56-
super(persistentEntity, auditorAware, auditDateAware, domainClass);
77+
@Override
78+
public Class<ID> getIdType() {
79+
if (null == persistentEntity) {
80+
return null;
81+
}
5782

58-
createdDateProperty = persistentEntity.getPersistentProperty(CreatedDate.class);
59-
lastModifiedDateProperty = persistentEntity.getPersistentProperty(LastModifiedDate.class);
60-
createdByProperty = persistentEntity.getPersistentProperty(CreatedBy.class);
61-
lastModifiedByProperty = persistentEntity.getPersistentProperty(LastModifiedBy.class);
83+
PersistentProperty idProperty = persistentEntity.getIdProperty();
84+
if (null == idProperty) {
85+
return null;
86+
}
87+
return (Class<ID>) idProperty.getType();
88+
}
89+
90+
@Override
91+
public boolean hasVersion() {
92+
return persistentEntity.hasVersionProperty();
93+
}
94+
95+
@Override
96+
public GenerationType getIdGenerationType() {
97+
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
98+
if (null == idProperty) {
99+
return null;
100+
}
101+
Id id = idProperty.findAnnotation(Id.class);
102+
if (null == id) {
103+
return null;
62104
}
63105

64-
@Override
65-
public ID getId(T entity) {
66-
if (null == persistentEntity) {
67-
return null;
68-
}
106+
return id.strategy();
69107

70-
return (ID) persistentEntity.getIdentifierAccessor(entity).getIdentifier();
108+
}
71109

110+
@Override
111+
public void setIdValue(T entity, ID id) {
112+
IdentifierAccessor identifierAccessor = persistentEntity.getIdentifierAccessor(entity);
113+
if (null == identifierAccessor) {
114+
return;
72115
}
73-
74-
@Override
75-
public Class<ID> getIdType() {
76-
if (null == persistentEntity) {
77-
return null;
78-
}
79-
80-
PersistentProperty idProperty = persistentEntity.getIdProperty();
81-
if (null == idProperty) {
82-
return null;
83-
}
84-
return (Class<ID>) idProperty.getType();
116+
Object identifier = identifierAccessor.getIdentifier();
117+
if (null != identifier) {
118+
return;
85119
}
86-
87-
@Override
88-
public boolean hasVersion() {
89-
return persistentEntity.hasVersionProperty();
120+
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
121+
if (null == idProperty) {
122+
return;
90123
}
124+
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty, id);
125+
}
91126

127+
private void setAuditDate(PersistentProperty<?> property, T entity,
128+
Class<? extends Annotation> annotationType) {
92129

93-
private void setAuditDate(PersistentProperty<?> property, T entity, Class<? extends Annotation> annotationType) {
94-
95-
Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property);
96-
if (null != val) {
97-
return;
98-
}
130+
Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property);
131+
if (null != val) {
132+
return;
133+
}
99134

135+
if (null != auditDateAware) {
136+
persistentEntity.getPropertyAccessor(entity)
137+
.setProperty(property, auditDateAware.getCurrentDate());
138+
return;
139+
}
100140

101-
if (null != auditDateAware) {
102-
persistentEntity.getPropertyAccessor(entity).setProperty(property, auditDateAware.getCurrentDate());
103-
return;
104-
}
141+
Class<?> type = property.getRawType();
142+
if (Date.class.isAssignableFrom(type)) {
143+
persistentEntity.getPropertyAccessor(entity).setProperty(property, new Date());
144+
} else if (Long.class == type || long.class == type) {
145+
persistentEntity.getPropertyAccessor(entity)
146+
.setProperty(property, System.currentTimeMillis());
147+
} else {
148+
throw new IllegalArgumentException(
149+
"now we can not support " + type.getName() + " for " + annotationType.getName()
150+
+ ", you can implement org.springframework.data.mybatis.domains.AuditDateAware interface as a spring bean.");
151+
}
152+
}
105153

106-
Class<?> type = property.getRawType();
107-
if (Date.class.isAssignableFrom(type)) {
108-
persistentEntity.getPropertyAccessor(entity).setProperty(property, new Date());
109-
} else if (Long.class == type || long.class == type) {
110-
persistentEntity.getPropertyAccessor(entity).setProperty(property, System.currentTimeMillis());
111-
} else {
112-
throw new IllegalArgumentException("now we can not support " + type.getName() + " for " + annotationType.getName()
113-
+ ", you can implement org.springframework.data.mybatis.domains.AuditDateAware interface as a spring bean.");
114-
}
154+
@Override
155+
public void setCreatedDate(T entity) {
156+
if (null == createdDateProperty) {
157+
return;
115158
}
159+
setAuditDate(createdDateProperty, entity, CreatedDate.class);
160+
}
116161

117-
@Override
118-
public void setCreatedDate(T entity) {
119-
if (null == createdDateProperty) {
120-
return;
121-
}
122-
setAuditDate(createdDateProperty, entity, CreatedDate.class);
162+
@Override
163+
public void setLastModifiedDate(T entity) {
164+
if (null == lastModifiedDateProperty) {
165+
return;
123166
}
167+
setAuditDate(lastModifiedDateProperty, entity, LastModifiedDate.class);
124168

125-
@Override
126-
public void setLastModifiedDate(T entity) {
127-
if (null == lastModifiedDateProperty) {
128-
return;
129-
}
130-
setAuditDate(lastModifiedDateProperty, entity, LastModifiedDate.class);
169+
}
131170

171+
private void setCurrentAuditor(PersistentProperty<?> property, T entity) {
172+
Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property);
173+
if (null != val) {
174+
return;
132175
}
133-
134-
private void setCurrentAuditor(PersistentProperty<?> property, T entity) {
135-
Object val = persistentEntity.getPropertyAccessor(entity).getProperty(property);
136-
if (null != val) {
137-
return;
138-
}
139-
persistentEntity.getPropertyAccessor(entity).setProperty(property, auditorAware.getCurrentAuditor());
176+
persistentEntity.getPropertyAccessor(entity)
177+
.setProperty(property, auditorAware.getCurrentAuditor());
178+
}
179+
180+
@Override
181+
public void setCreatedBy(T entity) {
182+
if (null == createdByProperty || null == auditorAware) {
183+
return;
140184
}
185+
setCurrentAuditor(createdByProperty, entity);
186+
}
141187

142-
@Override
143-
public void setCreatedBy(T entity) {
144-
if (null == createdByProperty || null == auditorAware) {
145-
return;
146-
}
147-
setCurrentAuditor(createdByProperty, entity);
188+
@Override
189+
public void setLastModifiedBy(T entity) {
190+
if (null == lastModifiedByProperty || null == auditorAware) {
191+
return;
148192
}
149-
150-
@Override
151-
public void setLastModifiedBy(T entity) {
152-
if (null == lastModifiedByProperty || null == auditorAware) {
153-
return;
154-
}
155-
setCurrentAuditor(lastModifiedByProperty, entity);
193+
setCurrentAuditor(lastModifiedByProperty, entity);
194+
}
195+
196+
@Override
197+
public int increaseVersion(T entity) {
198+
PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty();
199+
if (null == versionProperty) {
200+
return 0;
156201
}
157202

158-
@Override
159-
public int increaseVersion(T entity) {
160-
PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty();
161-
if (null == versionProperty) {
162-
return 0;
163-
}
203+
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(entity);
204+
int newVer = (Integer) accessor.getProperty(versionProperty) + 1;
205+
accessor.setProperty(versionProperty, newVer);
164206

165-
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(entity);
166-
int newVer = (Integer) accessor.getProperty(versionProperty) + 1;
167-
accessor.setProperty(versionProperty, newVer);
207+
return newVer;
208+
}
168209

169-
return newVer;
210+
@Override
211+
public void setVersion(T entity, int version) {
212+
PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty();
213+
if (null == versionProperty) {
214+
return;
170215
}
216+
persistentEntity.getPropertyAccessor(entity).setProperty(versionProperty, version);
171217

172-
@Override
173-
public void setVersion(T entity, int version) {
174-
PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty();
175-
if (null == versionProperty) {
176-
return;
177-
}
178-
persistentEntity.getPropertyAccessor(entity).setProperty(versionProperty, version);
179-
180-
}
218+
}
181219
}

0 commit comments

Comments
 (0)