Skip to content

Commit 3b6fba0

Browse files
author
Jarvis Song
authored
Merge pull request #98 from hatunet/dev
Dev
2 parents 296b3c5 + e158192 commit 3b6fba0

17 files changed

+843
-26
lines changed

src/main/java/org/springframework/data/mybatis/mapping/MybatisAssociation.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,16 @@ public MybatisPersistentEntity<?> getObversePersistentEntity() {
5555
return null;
5656
}
5757

58+
public MybatisPersistentEntity<?> getInversePersistentEntity() {
59+
60+
if (null != getInverse()) {
61+
MybatisPersistentEntity owner = (MybatisPersistentEntity) getInverse().getOwner();
62+
63+
return owner;
64+
65+
}
66+
return null;
67+
}
68+
69+
5870
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.springframework.data.mybatis.mapping;
2+
3+
/**
4+
* @author Jarvis Song
5+
*/
6+
public class MybatisManyToManyAssociation extends MybatisOneToManyAssociation {
7+
8+
public MybatisManyToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
9+
super(inverse, obverse);
10+
}
11+
12+
public boolean preferJoinTable() {
13+
if (null != joinColumn) {
14+
return false;
15+
}
16+
return true;
17+
}
18+
}

src/main/java/org/springframework/data/mybatis/mapping/MybatisManyToOneAssociation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public String getJoinColumnName() {
5252
} else {
5353
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
5454
if (null != entity && entity.hasIdProperty()) {
55-
name = ParsingUtils.reconcatenateCamelCase(getInverse().getName(), "_") + "_" + entity.getIdProperty().getColumnName();
55+
name = entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
5656
}
5757
}
5858
return name;
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package org.springframework.data.mybatis.mapping;
2+
3+
import org.springframework.data.mybatis.annotations.JoinColumn;
4+
import org.springframework.data.mybatis.annotations.JoinTable;
5+
import org.springframework.util.StringUtils;
6+
7+
/**
8+
* @author Jarvis Song
9+
*/
10+
public class MybatisOneToManyAssociation extends MybatisAssociation {
11+
12+
protected JoinColumn joinColumn;
13+
protected JoinTable joinTable;
14+
15+
public MybatisOneToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
16+
super(inverse, obverse);
17+
//user,booking
18+
if (null != inverse) {
19+
joinColumn = inverse.findAnnotation(JoinColumn.class);
20+
joinTable = inverse.findAnnotation(JoinTable.class);
21+
}
22+
}
23+
24+
public boolean preferJoinTable() {
25+
if (null != joinTable) {
26+
return true;
27+
}
28+
return false;
29+
}
30+
31+
public String getJoinTableName() {
32+
if (null != joinTable && StringUtils.hasText(joinTable.name())) {
33+
return joinTable.name();
34+
}
35+
36+
MybatisPersistentEntity<?> inversePersistentEntity = getInversePersistentEntity();
37+
MybatisPersistentEntity<?> obversePersistentEntity = getObversePersistentEntity();
38+
if (null != inversePersistentEntity && null != obversePersistentEntity) {
39+
return inversePersistentEntity.getTableName() + "_" + obversePersistentEntity.getTableName();
40+
}
41+
return null;
42+
}
43+
44+
public String[] getJoinTableJoinColumnNames() {
45+
if (null != joinTable) {
46+
if (null != joinTable.joinColumns() && joinTable.joinColumns().length > 0) {
47+
String[] result = new String[joinTable.joinColumns().length];
48+
for (int i = 0; i < joinTable.joinColumns().length; i++) {
49+
String name = joinTable.joinColumns()[i].name();
50+
if (StringUtils.isEmpty(name)) {
51+
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
52+
if (null != entity && entity.hasIdProperty()) {
53+
name = entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
54+
}
55+
}
56+
result[i] = name;
57+
}
58+
return result;
59+
}
60+
}
61+
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
62+
if (null != entity && entity.hasIdProperty()) {
63+
return new String[]{entity.getTableName() + "_" + entity.getIdProperty().getColumnName()};
64+
}
65+
return new String[0];
66+
}
67+
68+
public String[] getJoinTableJoinReferencedColumnNames() {
69+
if (null != joinTable) {
70+
if (null != joinTable.joinColumns() && joinTable.joinColumns().length > 0) {
71+
String[] result = new String[joinTable.joinColumns().length];
72+
for (int i = 0; i < joinTable.joinColumns().length; i++) {
73+
String name = joinTable.joinColumns()[i].referencedColumnName();
74+
if (StringUtils.isEmpty(name)) {
75+
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
76+
if (null != entity && entity.hasIdProperty()) {
77+
name = entity.getIdProperty().getColumnName();
78+
}
79+
}
80+
result[i] = name;
81+
}
82+
return result;
83+
}
84+
}
85+
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
86+
if (null != entity && entity.hasIdProperty()) {
87+
return new String[]{entity.getIdProperty().getColumnName()};
88+
}
89+
return new String[0];
90+
}
91+
92+
public String[] getJoinTableInverseJoinColumnNames() {
93+
if (null != joinTable) {
94+
if (null != joinTable.inverseJoinColumns() && joinTable.inverseJoinColumns().length > 0) {
95+
String[] result = new String[joinTable.inverseJoinColumns().length];
96+
for (int i = 0; i < joinTable.inverseJoinColumns().length; i++) {
97+
String name = joinTable.inverseJoinColumns()[i].name();
98+
if (StringUtils.isEmpty(name)) {
99+
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
100+
if (null != entity && entity.hasIdProperty()) {
101+
name = entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
102+
}
103+
}
104+
result[i] = name;
105+
}
106+
return result;
107+
}
108+
}
109+
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
110+
if (null != entity && entity.hasIdProperty()) {
111+
return new String[]{entity.getTableName() + "_" + entity.getIdProperty().getColumnName()};
112+
}
113+
return new String[0];
114+
}
115+
116+
public String[] getJoinTableInverseJoinReferencedColumnNames() {
117+
if (null != joinTable) {
118+
if (null != joinTable.inverseJoinColumns() && joinTable.inverseJoinColumns().length > 0) {
119+
String[] result = new String[joinTable.inverseJoinColumns().length];
120+
for (int i = 0; i < joinTable.inverseJoinColumns().length; i++) {
121+
String name = joinTable.inverseJoinColumns()[i].referencedColumnName();
122+
if (StringUtils.isEmpty(name)) {
123+
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
124+
if (null != entity && entity.hasIdProperty()) {
125+
name = entity.getIdProperty().getColumnName();
126+
}
127+
}
128+
result[i] = name;
129+
}
130+
return result;
131+
}
132+
}
133+
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
134+
if (null != entity && entity.hasIdProperty()) {
135+
return new String[]{entity.getIdProperty().getColumnName()};
136+
}
137+
return new String[0];
138+
}
139+
140+
/**
141+
* @return
142+
*/
143+
public String getJoinColumnName() {
144+
//user_id(booking's)
145+
if (null != joinColumn && StringUtils.hasText(joinColumn.name())) {
146+
return joinColumn.name();
147+
}
148+
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
149+
if (null != entity && entity.hasIdProperty()) {
150+
return entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
151+
}
152+
153+
return null;
154+
}
155+
156+
public String getJoinReferencedColumnName() {
157+
//id (user's)
158+
159+
if (null != joinColumn && StringUtils.hasText(joinColumn.referencedColumnName())) {
160+
return joinColumn.referencedColumnName();
161+
}
162+
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
163+
if (null != entity && entity.hasIdProperty()) {
164+
return entity.getIdProperty().getColumnName();
165+
}
166+
167+
return null;
168+
}
169+
170+
@Override
171+
public MybatisPersistentProperty getObverse() {
172+
173+
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
174+
if (null == entity) {
175+
return null;
176+
}
177+
if (null == joinColumn || StringUtils.isEmpty(joinColumn.referencedColumnName())) {
178+
return entity.getIdProperty();
179+
}
180+
181+
return entity.findByColumnName(joinColumn.referencedColumnName());
182+
183+
}
184+
185+
186+
}

src/main/java/org/springframework/data/mybatis/mapping/MybatisPersistentPropertyImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ protected Association<MybatisPersistentProperty> createAssociation() {
9898
return new MybatisOneToOneAssociation(this, null);
9999
}
100100

101+
if (null != findAnnotation(OneToMany.class)) {
102+
return new MybatisOneToManyAssociation(this, null);
103+
}
104+
105+
if (null != findAnnotation(ManyToMany.class)) {
106+
return new MybatisManyToManyAssociation(this, null);
107+
}
108+
101109
return new MybatisAssociation(this, null);
102110

103111
}

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

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import org.springframework.data.mapping.PersistentProperty;
2424
import org.springframework.data.mapping.SimpleAssociationHandler;
2525
import org.springframework.data.mapping.SimplePropertyHandler;
26-
import org.springframework.data.mybatis.mapping.MybatisEmbeddedAssociation;
27-
import org.springframework.data.mybatis.mapping.MybatisManyToOneAssociation;
28-
import org.springframework.data.mybatis.mapping.MybatisPersistentEntity;
29-
import org.springframework.data.mybatis.mapping.MybatisPersistentProperty;
26+
import org.springframework.data.mybatis.mapping.*;
3027
import org.springframework.data.mybatis.repository.dialect.Dialect;
3128
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
3229
import org.springframework.data.repository.query.parser.Part.Type;
@@ -240,6 +237,52 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
240237
}
241238
}
242239

240+
if ((ass instanceof MybatisOneToManyAssociation)) {
241+
if (basic) {
242+
return;
243+
}
244+
245+
final MybatisOneToManyAssociation association = (MybatisOneToManyAssociation) ass;
246+
MybatisPersistentEntity<?> obversePersistentEntity = association.getObversePersistentEntity();
247+
if (null != obversePersistentEntity) {
248+
obversePersistentEntity.doWithProperties(new SimplePropertyHandler() {
249+
@Override
250+
public void doWithPersistentProperty(PersistentProperty<?> pp) {
251+
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
252+
builder.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()) + "." + dialect.wrapColumnName(property.getColumnName()))
253+
.append(" as ").append(quota(association.getInverse().getName() + "." + property.getName())).append(",");
254+
}
255+
});
256+
obversePersistentEntity.doWithAssociations(new SimpleAssociationHandler() {
257+
@Override
258+
public void doWithAssociation(Association<? extends PersistentProperty<?>> ass) {
259+
if ((ass instanceof MybatisEmbeddedAssociation)) {
260+
final MybatisEmbeddedAssociation association1 = (MybatisEmbeddedAssociation) ass;
261+
MybatisPersistentEntity<?> obversePersistentEntity1 = association1.getObversePersistentEntity();
262+
if (null != obversePersistentEntity1) {
263+
obversePersistentEntity1.doWithProperties(new SimplePropertyHandler() {
264+
@Override
265+
public void doWithPersistentProperty(PersistentProperty<?> pp) {
266+
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
267+
builder.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()) + "." + dialect.wrapColumnName(property.getColumnName()))
268+
.append(" as ").append(quota(association.getInverse().getName() + "." + association1.getInverse().getName() + "." + property.getName())).append(",");
269+
}
270+
});
271+
}
272+
return;
273+
}
274+
275+
if ((ass instanceof MybatisManyToOneAssociation)) {
276+
final MybatisManyToOneAssociation association1 = (MybatisManyToOneAssociation) ass;
277+
builder.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()) + "." + dialect.wrapColumnName(association1.getJoinColumnName()))
278+
.append(" as ").append(quota(association.getInverse().getName() + "." + association1.getInverse().getName() + "." + association1.getObverse().getName())).append(",");
279+
280+
}
281+
}
282+
});
283+
}
284+
285+
}
243286
}
244287
});
245288

@@ -269,10 +312,50 @@ private String buildLeftOuterJoin() {
269312
public void doWithAssociation(Association<? extends PersistentProperty<?>> ass) {
270313
if ((ass instanceof MybatisManyToOneAssociation)) {
271314
final MybatisManyToOneAssociation association = (MybatisManyToOneAssociation) ass;
272-
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ").append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
315+
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ")
316+
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
273317
.append(" on ").append(quota(persistentEntity.getEntityName())).append(".").append(dialect.wrapColumnName(association.getJoinColumnName()))
274318
.append("=").append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName())).append(".").append(dialect.wrapColumnName(association.getJoinReferencedColumnName()));
319+
return;
320+
}
321+
322+
if ((ass instanceof MybatisOneToManyAssociation)) {
323+
final MybatisOneToManyAssociation association = (MybatisOneToManyAssociation) ass;
324+
325+
if (association.preferJoinTable()) {
326+
// join table
327+
builder.append(" left outer join ").append(dialect.wrapTableName(association.getJoinTableName())).append(" ")
328+
.append(" on ");
329+
String[] joinTableJoinColumnNames = association.getJoinTableJoinColumnNames();
330+
String[] joinTableJoinReferencedColumnNames = association.getJoinTableJoinReferencedColumnNames();
331+
for (int i = 0; i < joinTableJoinColumnNames.length; i++) {
332+
if (i > 0) {
333+
builder.append(" and ");
334+
}
335+
builder.append(dialect.wrapTableName(association.getJoinTableName())).append(".").append(joinTableJoinColumnNames[i]).append("=")
336+
.append(quota(persistentEntity.getEntityName())).append(".").append(dialect.wrapColumnName(joinTableJoinReferencedColumnNames[i]));
337+
}
338+
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ")
339+
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
340+
.append(" on ");
341+
String[] joinTableInverseJoinColumnNames = association.getJoinTableInverseJoinColumnNames();
342+
String[] joinTableInverseJoinReferencedColumnNames = association.getJoinTableInverseJoinReferencedColumnNames();
343+
for (int i = 0; i < joinTableInverseJoinColumnNames.length; i++) {
344+
if (i > 0) {
345+
builder.append(" and ");
346+
}
347+
builder.append(dialect.wrapTableName(association.getJoinTableName())).append(".").append(joinTableInverseJoinColumnNames[i]).append("=")
348+
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName())).append(".").append(dialect.wrapColumnName(joinTableInverseJoinReferencedColumnNames[i]));
349+
}
350+
} else {
351+
// join column
352+
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ")
353+
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
354+
.append(" on ").append(quota(persistentEntity.getEntityName())).append(".").append(dialect.wrapColumnName(association.getJoinReferencedColumnName()))
355+
.append("=").append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName())).append(".").append(dialect.wrapColumnName(association.getJoinColumnName()));
356+
}
275357
}
358+
276359
}
277360
});
278361

0 commit comments

Comments
 (0)