Skip to content

Commit 29c7ec0

Browse files
authored
Make it possible to include or exclude embedded properties (#1048)
1 parent 8ce9a33 commit 29c7ec0

File tree

7 files changed

+255
-2
lines changed

7 files changed

+255
-2
lines changed

doma-processor/src/main/java/org/seasar/doma/internal/apt/annot/Annotations.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public EmbeddableAnnot newEmbeddableAnnot(TypeElement typeElement) {
166166
assertNotNull(typeElement);
167167
AnnotationMirror embeddableMirror =
168168
ctx.getMoreElements().getAnnotationMirror(typeElement, Embeddable.class);
169+
if (embeddableMirror == null) {
170+
return null;
171+
}
169172
Map<String, AnnotationValue> valuesWithoutDefaults =
170173
ctx.getMoreElements().getValuesWithoutDefaults(embeddableMirror);
171174
AnnotationValue metamodel = valuesWithoutDefaults.get(EmbeddableAnnot.METAMODEL);

doma-processor/src/main/java/org/seasar/doma/internal/apt/meta/entity/EntityPropertyNameCollector.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import javax.lang.model.util.ElementFilter;
1313
import org.seasar.doma.Transient;
1414
import org.seasar.doma.internal.apt.Context;
15+
import org.seasar.doma.internal.apt.annot.EmbeddableAnnot;
1516

1617
public class EntityPropertyNameCollector {
1718

@@ -33,8 +34,20 @@ private void collectNames(TypeMirror type, Set<String> names) {
3334
t != null && t.asType().getKind() != TypeKind.NONE;
3435
t = ctx.getMoreTypes().toTypeElement(t.getSuperclass())) {
3536
for (VariableElement field : ElementFilter.fieldsIn(t.getEnclosedElements())) {
36-
if (isPersistent(field)) {
37-
names.add(field.getSimpleName().toString());
37+
TypeElement filedTypeElement = ctx.getMoreTypes().toTypeElement(field.asType());
38+
EmbeddableAnnot embeddableAnnot = ctx.getAnnotations().newEmbeddableAnnot(filedTypeElement);
39+
String name = field.getSimpleName().toString();
40+
if (embeddableAnnot == null) {
41+
if (isPersistent(field)) {
42+
names.add(name);
43+
}
44+
} else {
45+
EmbeddableMetaFactory embeddableMetaFactory = new EmbeddableMetaFactory(ctx);
46+
EmbeddableMeta embeddableMeta =
47+
embeddableMetaFactory.createTypeElementMeta(filedTypeElement);
48+
for (EmbeddablePropertyMeta propertyMeta : embeddableMeta.getEmbeddablePropertyMetas()) {
49+
names.add(name + "." + propertyMeta.getName());
50+
}
3851
}
3952
}
4053
}

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/dao/IncludeAndExcludeDao.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.seasar.doma.Dao;
44
import org.seasar.doma.Update;
55
import org.seasar.doma.internal.apt.processor.entity.Emp;
6+
import org.seasar.doma.internal.apt.processor.entity.ImmutableUser;
7+
import org.seasar.doma.jdbc.Result;
68

79
@SuppressWarnings("deprecation")
810
@Dao(config = MyConfig.class)
@@ -12,4 +14,9 @@ public interface IncludeAndExcludeDao {
1214
include = {"name", "salary"},
1315
exclude = {"salary"})
1416
int update(Emp emp);
17+
18+
@Update(
19+
include = {"address.city"},
20+
exclude = {"address.street"})
21+
Result<ImmutableUser> update(ImmutableUser user);
1522
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package org.seasar.doma.internal.apt.processor.entity;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.function.BiFunction;
6+
import org.seasar.doma.jdbc.entity.AbstractEntityType;
7+
import org.seasar.doma.jdbc.entity.EntityPropertyType;
8+
import org.seasar.doma.jdbc.entity.GeneratedIdPropertyType;
9+
import org.seasar.doma.jdbc.entity.NamingType;
10+
import org.seasar.doma.jdbc.entity.PostDeleteContext;
11+
import org.seasar.doma.jdbc.entity.PostInsertContext;
12+
import org.seasar.doma.jdbc.entity.PostUpdateContext;
13+
import org.seasar.doma.jdbc.entity.PreDeleteContext;
14+
import org.seasar.doma.jdbc.entity.PreInsertContext;
15+
import org.seasar.doma.jdbc.entity.PreUpdateContext;
16+
import org.seasar.doma.jdbc.entity.Property;
17+
import org.seasar.doma.jdbc.entity.TenantIdPropertyType;
18+
import org.seasar.doma.jdbc.entity.VersionPropertyType;
19+
20+
public class _ImmutableUser extends AbstractEntityType<ImmutableUser> {
21+
22+
@Override
23+
public String getCatalogName() {
24+
25+
return null;
26+
}
27+
28+
@Override
29+
public Class<ImmutableUser> getEntityClass() {
30+
31+
return null;
32+
}
33+
34+
@Override
35+
public EntityPropertyType<ImmutableUser, ?> getEntityPropertyType(String name) {
36+
37+
return null;
38+
}
39+
40+
@Override
41+
public List<EntityPropertyType<ImmutableUser, ?>> getEntityPropertyTypes() {
42+
43+
return null;
44+
}
45+
46+
@Override
47+
public GeneratedIdPropertyType<ImmutableUser, ?, ?> getGeneratedIdPropertyType() {
48+
49+
return null;
50+
}
51+
52+
@Override
53+
public List<EntityPropertyType<ImmutableUser, ?>> getIdPropertyTypes() {
54+
55+
return null;
56+
}
57+
58+
@Override
59+
public String getName() {
60+
61+
return null;
62+
}
63+
64+
@Override
65+
public ImmutableUser getOriginalStates(ImmutableUser entity) {
66+
67+
return null;
68+
}
69+
70+
@Override
71+
public String getSchemaName() {
72+
73+
return null;
74+
}
75+
76+
@Override
77+
@Deprecated
78+
public String getTableName() {
79+
80+
return null;
81+
}
82+
83+
@Override
84+
public String getTableName(BiFunction<NamingType, String, String> namingFunction) {
85+
86+
return null;
87+
}
88+
89+
@Override
90+
public VersionPropertyType<ImmutableUser, ?, ?> getVersionPropertyType() {
91+
92+
return null;
93+
}
94+
95+
@Override
96+
public TenantIdPropertyType<ImmutableUser, ?, ?> getTenantIdPropertyType() {
97+
return null;
98+
}
99+
100+
@Override
101+
public void preDelete(ImmutableUser entity, PreDeleteContext<ImmutableUser> context) {}
102+
103+
@Override
104+
public void preInsert(ImmutableUser entity, PreInsertContext<ImmutableUser> context) {}
105+
106+
@Override
107+
public void preUpdate(ImmutableUser entity, PreUpdateContext<ImmutableUser> context) {}
108+
109+
@Override
110+
public void postDelete(ImmutableUser entity, PostDeleteContext<ImmutableUser> context) {}
111+
112+
@Override
113+
public void postInsert(ImmutableUser entity, PostInsertContext<ImmutableUser> context) {}
114+
115+
@Override
116+
public void postUpdate(ImmutableUser entity, PostUpdateContext<ImmutableUser> context) {}
117+
118+
@Override
119+
public void saveCurrentStates(ImmutableUser entity) {}
120+
121+
@Override
122+
public NamingType getNamingType() {
123+
return null;
124+
}
125+
126+
public static _ImmutableUser getSingletonInternal() {
127+
return null;
128+
}
129+
130+
@Override
131+
public boolean isImmutable() {
132+
return false;
133+
}
134+
135+
@Override
136+
public ImmutableUser newEntity(Map<String, Property<ImmutableUser, ?>> args) {
137+
return null;
138+
}
139+
140+
@Override
141+
public boolean isQuoteRequired() {
142+
return false;
143+
}
144+
}

doma-processor/src/test/resources/org/seasar/doma/internal/apt/processor/dao/DaoProcessorTest_IncludeAndExcludeDao.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class IncludeAndExcludeDaoImpl implements org.seasar.doma.internal.apt.pr
1111

1212
private static final java.lang.reflect.Method __method0 = org.seasar.doma.internal.jdbc.dao.DaoImplSupport.getDeclaredMethod(org.seasar.doma.internal.apt.processor.dao.IncludeAndExcludeDao.class, "update", org.seasar.doma.internal.apt.processor.entity.Emp.class);
1313

14+
private static final java.lang.reflect.Method __method1 = org.seasar.doma.internal.jdbc.dao.DaoImplSupport.getDeclaredMethod(org.seasar.doma.internal.apt.processor.dao.IncludeAndExcludeDao.class, "update", org.seasar.doma.internal.apt.processor.entity.ImmutableUser.class);
15+
1416
private final org.seasar.doma.internal.jdbc.dao.DaoImplSupport __support;
1517

1618
/** */
@@ -93,4 +95,38 @@ public class IncludeAndExcludeDaoImpl implements org.seasar.doma.internal.apt.pr
9395
}
9496
}
9597

98+
@Override
99+
public org.seasar.doma.jdbc.Result<org.seasar.doma.internal.apt.processor.entity.ImmutableUser> update(org.seasar.doma.internal.apt.processor.entity.ImmutableUser user) {
100+
__support.entering("org.seasar.doma.internal.apt.processor.dao.IncludeAndExcludeDaoImpl", "update", user);
101+
try {
102+
if (user == null) {
103+
throw new org.seasar.doma.DomaNullPointerException("user");
104+
}
105+
org.seasar.doma.jdbc.query.AutoUpdateQuery<org.seasar.doma.internal.apt.processor.entity.ImmutableUser> __query = __support.getQueryImplementors().createAutoUpdateQuery(__method1, org.seasar.doma.internal.apt.processor.entity._ImmutableUser.getSingletonInternal());
106+
__query.setMethod(__method1);
107+
__query.setConfig(__support.getConfig());
108+
__query.setEntity(user);
109+
__query.setCallerClassName("org.seasar.doma.internal.apt.processor.dao.IncludeAndExcludeDaoImpl");
110+
__query.setCallerMethodName("update");
111+
__query.setQueryTimeout(-1);
112+
__query.setSqlLogType(org.seasar.doma.jdbc.SqlLogType.FORMATTED);
113+
__query.setNullExcluded(false);
114+
__query.setVersionIgnored(false);
115+
__query.setIncludedPropertyNames("address.city");
116+
__query.setExcludedPropertyNames("address.street");
117+
__query.setUnchangedPropertyIncluded(false);
118+
__query.setOptimisticLockExceptionSuppressed(false);
119+
__query.prepare();
120+
org.seasar.doma.jdbc.command.UpdateCommand __command = __support.getCommandImplementors().createUpdateCommand(__method1, __query);
121+
int __count = __command.execute();
122+
__query.complete();
123+
org.seasar.doma.jdbc.Result<org.seasar.doma.internal.apt.processor.entity.ImmutableUser> __result = new org.seasar.doma.jdbc.Result<org.seasar.doma.internal.apt.processor.entity.ImmutableUser>(__count, __query.getEntity());
124+
__support.exiting("org.seasar.doma.internal.apt.processor.dao.IncludeAndExcludeDaoImpl", "update", __result);
125+
return __result;
126+
} catch (java.lang.RuntimeException __e) {
127+
__support.throwing("org.seasar.doma.internal.apt.processor.dao.IncludeAndExcludeDaoImpl", "update", __e);
128+
throw __e;
129+
}
130+
}
131+
96132
}

integration-test-java/src/main/java/org/seasar/doma/it/dao/StaffDao.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public interface StaffDao {
2323
@Update
2424
int update(Staff staff);
2525

26+
@Update(include = "staffInfo.salary")
27+
int updateSalary(Staff staff);
28+
29+
@Update(exclude = "staffInfo.salary")
30+
int updateExceptSalary(Staff staff);
31+
2632
@Update(sqlFile = true)
2733
int updateBySqlFile(Staff staff);
2834

integration-test-java/src/test/java/org/seasar/doma/it/auto/AutoUpdateTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,48 @@ public void testTenantId(Config config) throws Exception {
304304
salesman.departmentId = tenantId;
305305
dao.update(salesman);
306306
}
307+
308+
@Test
309+
public void testUpdate_IncludeEmbeddedProperty(Config config) throws Exception {
310+
StaffDao dao = new StaffDaoImpl(config);
311+
Staff staff = dao.selectById(1);
312+
staff.employeeName = "hoge";
313+
staff.staffInfo =
314+
new StaffInfo(staff.staffInfo.managerId, staff.staffInfo.hiredate, new Salary("5000"));
315+
int result = dao.updateSalary(staff);
316+
assertEquals(1, result);
317+
assertEquals(2, staff.version.intValue());
318+
319+
staff = dao.selectById(1);
320+
assertEquals(7369, staff.employeeNo.intValue());
321+
assertEquals(2, staff.version.intValue());
322+
assertEquals("SMITH", staff.employeeName);
323+
assertEquals(5000L, staff.staffInfo.salary.getValue().longValue());
324+
assertEquals(java.sql.Date.valueOf("1980-12-17"), staff.staffInfo.hiredate);
325+
assertEquals(13, staff.staffInfo.managerId);
326+
assertEquals(2, staff.departmentId.intValue());
327+
assertEquals(1, staff.addressId.intValue());
328+
}
329+
330+
@Test
331+
public void testUpdate_ExcludeEmbeddedProperty(Config config) throws Exception {
332+
StaffDao dao = new StaffDaoImpl(config);
333+
Staff staff = dao.selectById(1);
334+
staff.employeeName = "hoge";
335+
staff.staffInfo =
336+
new StaffInfo(staff.staffInfo.managerId, staff.staffInfo.hiredate, new Salary("5000"));
337+
int result = dao.updateExceptSalary(staff);
338+
assertEquals(1, result);
339+
assertEquals(2, staff.version.intValue());
340+
341+
staff = dao.selectById(1);
342+
assertEquals(7369, staff.employeeNo.intValue());
343+
assertEquals(2, staff.version.intValue());
344+
assertEquals("hoge", staff.employeeName);
345+
assertEquals(800L, staff.staffInfo.salary.getValue().longValue());
346+
assertEquals(java.sql.Date.valueOf("1980-12-17"), staff.staffInfo.hiredate);
347+
assertEquals(13, staff.staffInfo.managerId);
348+
assertEquals(2, staff.departmentId.intValue());
349+
assertEquals(1, staff.addressId.intValue());
350+
}
307351
}

0 commit comments

Comments
 (0)