Skip to content

Commit e158192

Browse files
author
Jarvis
committed
closed #94 #95 support @onetomany
1 parent c2a1d9d commit e158192

File tree

15 files changed

+744
-21
lines changed

15 files changed

+744
-21
lines changed

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

+12
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
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ public class MybatisManyToManyAssociation extends MybatisOneToManyAssociation {
88
public MybatisManyToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
99
super(inverse, obverse);
1010
}
11+
12+
public boolean preferJoinTable() {
13+
if (null != joinColumn) {
14+
return false;
15+
}
16+
return true;
17+
}
1118
}

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

+1-1
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;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,186 @@
11
package org.springframework.data.mybatis.mapping;
22

3+
import org.springframework.data.mybatis.annotations.JoinColumn;
4+
import org.springframework.data.mybatis.annotations.JoinTable;
5+
import org.springframework.util.StringUtils;
6+
37
/**
48
* @author Jarvis Song
59
*/
610
public class MybatisOneToManyAssociation extends MybatisAssociation {
711

12+
protected JoinColumn joinColumn;
13+
protected JoinTable joinTable;
14+
815
public MybatisOneToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
916
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];
10138
}
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+
11186
}

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

+88-5
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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.springframework.data.mybatis.domain.sample;
2+
3+
import org.springframework.data.annotation.PersistenceConstructor;
4+
import org.springframework.data.mybatis.annotations.Entity;
5+
import org.springframework.data.mybatis.domains.LongId;
6+
7+
/**
8+
* @author Jarvis Song
9+
*/
10+
@Entity(table = "ds_booking")
11+
public class Booking extends LongId {
12+
13+
private Long userId;
14+
private String serialNumber;
15+
private Integer amount;
16+
17+
public Booking() {
18+
}
19+
20+
@PersistenceConstructor
21+
public Booking(Long userId) {
22+
this.userId = userId;
23+
}
24+
25+
public Long getUserId() {
26+
return userId;
27+
}
28+
29+
public void setUserId(Long userId) {
30+
this.userId = userId;
31+
}
32+
33+
public String getSerialNumber() {
34+
return serialNumber;
35+
}
36+
37+
public void setSerialNumber(String serialNumber) {
38+
this.serialNumber = serialNumber;
39+
}
40+
41+
public Integer getAmount() {
42+
return amount;
43+
}
44+
45+
public void setAmount(Integer amount) {
46+
this.amount = amount;
47+
}
48+
}

src/test/java/org/springframework/data/mybatis/domain/sample/Role.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class Role {
4040
private String name;
4141

4242
@ManyToOne
43+
@JoinColumn(name = "group_id")
4344
private Group group;
4445

4546
/**

0 commit comments

Comments
 (0)