|
1 | 1 | package org.springframework.data.mybatis.mapping;
|
2 | 2 |
|
| 3 | +import org.springframework.data.mybatis.annotations.JoinColumn; |
| 4 | +import org.springframework.data.mybatis.annotations.JoinTable; |
| 5 | +import org.springframework.util.StringUtils; |
| 6 | + |
3 | 7 | /**
|
4 | 8 | * @author Jarvis Song
|
5 | 9 | */
|
6 | 10 | public class MybatisOneToManyAssociation extends MybatisAssociation {
|
7 | 11 |
|
| 12 | + protected JoinColumn joinColumn; |
| 13 | + protected JoinTable joinTable; |
| 14 | + |
8 | 15 | public MybatisOneToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
|
9 | 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]; |
10 | 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 | + |
11 | 186 | }
|
0 commit comments