Skip to content

Dev #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 1, 2017
Merged

Dev #98

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ public MybatisPersistentEntity<?> getObversePersistentEntity() {
return null;
}

public MybatisPersistentEntity<?> getInversePersistentEntity() {

if (null != getInverse()) {
MybatisPersistentEntity owner = (MybatisPersistentEntity) getInverse().getOwner();

return owner;

}
return null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.springframework.data.mybatis.mapping;

/**
* @author Jarvis Song
*/
public class MybatisManyToManyAssociation extends MybatisOneToManyAssociation {

public MybatisManyToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
super(inverse, obverse);
}

public boolean preferJoinTable() {
if (null != joinColumn) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getJoinColumnName() {
} else {
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
name = ParsingUtils.reconcatenateCamelCase(getInverse().getName(), "_") + "_" + entity.getIdProperty().getColumnName();
name = entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
}
}
return name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package org.springframework.data.mybatis.mapping;

import org.springframework.data.mybatis.annotations.JoinColumn;
import org.springframework.data.mybatis.annotations.JoinTable;
import org.springframework.util.StringUtils;

/**
* @author Jarvis Song
*/
public class MybatisOneToManyAssociation extends MybatisAssociation {

protected JoinColumn joinColumn;
protected JoinTable joinTable;

public MybatisOneToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
super(inverse, obverse);
//user,booking
if (null != inverse) {
joinColumn = inverse.findAnnotation(JoinColumn.class);
joinTable = inverse.findAnnotation(JoinTable.class);
}
}

public boolean preferJoinTable() {
if (null != joinTable) {
return true;
}
return false;
}

public String getJoinTableName() {
if (null != joinTable && StringUtils.hasText(joinTable.name())) {
return joinTable.name();
}

MybatisPersistentEntity<?> inversePersistentEntity = getInversePersistentEntity();
MybatisPersistentEntity<?> obversePersistentEntity = getObversePersistentEntity();
if (null != inversePersistentEntity && null != obversePersistentEntity) {
return inversePersistentEntity.getTableName() + "_" + obversePersistentEntity.getTableName();
}
return null;
}

public String[] getJoinTableJoinColumnNames() {
if (null != joinTable) {
if (null != joinTable.joinColumns() && joinTable.joinColumns().length > 0) {
String[] result = new String[joinTable.joinColumns().length];
for (int i = 0; i < joinTable.joinColumns().length; i++) {
String name = joinTable.joinColumns()[i].name();
if (StringUtils.isEmpty(name)) {
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
name = entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
}
}
result[i] = name;
}
return result;
}
}
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
return new String[]{entity.getTableName() + "_" + entity.getIdProperty().getColumnName()};
}
return new String[0];
}

public String[] getJoinTableJoinReferencedColumnNames() {
if (null != joinTable) {
if (null != joinTable.joinColumns() && joinTable.joinColumns().length > 0) {
String[] result = new String[joinTable.joinColumns().length];
for (int i = 0; i < joinTable.joinColumns().length; i++) {
String name = joinTable.joinColumns()[i].referencedColumnName();
if (StringUtils.isEmpty(name)) {
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
name = entity.getIdProperty().getColumnName();
}
}
result[i] = name;
}
return result;
}
}
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
return new String[]{entity.getIdProperty().getColumnName()};
}
return new String[0];
}

public String[] getJoinTableInverseJoinColumnNames() {
if (null != joinTable) {
if (null != joinTable.inverseJoinColumns() && joinTable.inverseJoinColumns().length > 0) {
String[] result = new String[joinTable.inverseJoinColumns().length];
for (int i = 0; i < joinTable.inverseJoinColumns().length; i++) {
String name = joinTable.inverseJoinColumns()[i].name();
if (StringUtils.isEmpty(name)) {
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
name = entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
}
}
result[i] = name;
}
return result;
}
}
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
return new String[]{entity.getTableName() + "_" + entity.getIdProperty().getColumnName()};
}
return new String[0];
}

public String[] getJoinTableInverseJoinReferencedColumnNames() {
if (null != joinTable) {
if (null != joinTable.inverseJoinColumns() && joinTable.inverseJoinColumns().length > 0) {
String[] result = new String[joinTable.inverseJoinColumns().length];
for (int i = 0; i < joinTable.inverseJoinColumns().length; i++) {
String name = joinTable.inverseJoinColumns()[i].referencedColumnName();
if (StringUtils.isEmpty(name)) {
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
name = entity.getIdProperty().getColumnName();
}
}
result[i] = name;
}
return result;
}
}
MybatisPersistentEntity<?> entity = getObversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
return new String[]{entity.getIdProperty().getColumnName()};
}
return new String[0];
}

/**
* @return
*/
public String getJoinColumnName() {
//user_id(booking's)
if (null != joinColumn && StringUtils.hasText(joinColumn.name())) {
return joinColumn.name();
}
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
return entity.getTableName() + "_" + entity.getIdProperty().getColumnName();
}

return null;
}

public String getJoinReferencedColumnName() {
//id (user's)

if (null != joinColumn && StringUtils.hasText(joinColumn.referencedColumnName())) {
return joinColumn.referencedColumnName();
}
MybatisPersistentEntity<?> entity = getInversePersistentEntity();
if (null != entity && entity.hasIdProperty()) {
return entity.getIdProperty().getColumnName();
}

return null;
}

@Override
public MybatisPersistentProperty getObverse() {

MybatisPersistentEntity<?> entity = getObversePersistentEntity();
if (null == entity) {
return null;
}
if (null == joinColumn || StringUtils.isEmpty(joinColumn.referencedColumnName())) {
return entity.getIdProperty();
}

return entity.findByColumnName(joinColumn.referencedColumnName());

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ protected Association<MybatisPersistentProperty> createAssociation() {
return new MybatisOneToOneAssociation(this, null);
}

if (null != findAnnotation(OneToMany.class)) {
return new MybatisOneToManyAssociation(this, null);
}

if (null != findAnnotation(ManyToMany.class)) {
return new MybatisManyToManyAssociation(this, null);
}

return new MybatisAssociation(this, null);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.mybatis.mapping.MybatisEmbeddedAssociation;
import org.springframework.data.mybatis.mapping.MybatisManyToOneAssociation;
import org.springframework.data.mybatis.mapping.MybatisPersistentEntity;
import org.springframework.data.mybatis.mapping.MybatisPersistentProperty;
import org.springframework.data.mybatis.mapping.*;
import org.springframework.data.mybatis.repository.dialect.Dialect;
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
import org.springframework.data.repository.query.parser.Part.Type;
Expand Down Expand Up @@ -240,6 +237,52 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
}
}

if ((ass instanceof MybatisOneToManyAssociation)) {
if (basic) {
return;
}

final MybatisOneToManyAssociation association = (MybatisOneToManyAssociation) ass;
MybatisPersistentEntity<?> obversePersistentEntity = association.getObversePersistentEntity();
if (null != obversePersistentEntity) {
obversePersistentEntity.doWithProperties(new SimplePropertyHandler() {
@Override
public void doWithPersistentProperty(PersistentProperty<?> pp) {
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
builder.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()) + "." + dialect.wrapColumnName(property.getColumnName()))
.append(" as ").append(quota(association.getInverse().getName() + "." + property.getName())).append(",");
}
});
obversePersistentEntity.doWithAssociations(new SimpleAssociationHandler() {
@Override
public void doWithAssociation(Association<? extends PersistentProperty<?>> ass) {
if ((ass instanceof MybatisEmbeddedAssociation)) {
final MybatisEmbeddedAssociation association1 = (MybatisEmbeddedAssociation) ass;
MybatisPersistentEntity<?> obversePersistentEntity1 = association1.getObversePersistentEntity();
if (null != obversePersistentEntity1) {
obversePersistentEntity1.doWithProperties(new SimplePropertyHandler() {
@Override
public void doWithPersistentProperty(PersistentProperty<?> pp) {
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
builder.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()) + "." + dialect.wrapColumnName(property.getColumnName()))
.append(" as ").append(quota(association.getInverse().getName() + "." + association1.getInverse().getName() + "." + property.getName())).append(",");
}
});
}
return;
}

if ((ass instanceof MybatisManyToOneAssociation)) {
final MybatisManyToOneAssociation association1 = (MybatisManyToOneAssociation) ass;
builder.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()) + "." + dialect.wrapColumnName(association1.getJoinColumnName()))
.append(" as ").append(quota(association.getInverse().getName() + "." + association1.getInverse().getName() + "." + association1.getObverse().getName())).append(",");

}
}
});
}

}
}
});

Expand Down Expand Up @@ -269,10 +312,50 @@ private String buildLeftOuterJoin() {
public void doWithAssociation(Association<? extends PersistentProperty<?>> ass) {
if ((ass instanceof MybatisManyToOneAssociation)) {
final MybatisManyToOneAssociation association = (MybatisManyToOneAssociation) ass;
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ").append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ")
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
.append(" on ").append(quota(persistentEntity.getEntityName())).append(".").append(dialect.wrapColumnName(association.getJoinColumnName()))
.append("=").append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName())).append(".").append(dialect.wrapColumnName(association.getJoinReferencedColumnName()));
return;
}

if ((ass instanceof MybatisOneToManyAssociation)) {
final MybatisOneToManyAssociation association = (MybatisOneToManyAssociation) ass;

if (association.preferJoinTable()) {
// join table
builder.append(" left outer join ").append(dialect.wrapTableName(association.getJoinTableName())).append(" ")
.append(" on ");
String[] joinTableJoinColumnNames = association.getJoinTableJoinColumnNames();
String[] joinTableJoinReferencedColumnNames = association.getJoinTableJoinReferencedColumnNames();
for (int i = 0; i < joinTableJoinColumnNames.length; i++) {
if (i > 0) {
builder.append(" and ");
}
builder.append(dialect.wrapTableName(association.getJoinTableName())).append(".").append(joinTableJoinColumnNames[i]).append("=")
.append(quota(persistentEntity.getEntityName())).append(".").append(dialect.wrapColumnName(joinTableJoinReferencedColumnNames[i]));
}
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ")
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
.append(" on ");
String[] joinTableInverseJoinColumnNames = association.getJoinTableInverseJoinColumnNames();
String[] joinTableInverseJoinReferencedColumnNames = association.getJoinTableInverseJoinReferencedColumnNames();
for (int i = 0; i < joinTableInverseJoinColumnNames.length; i++) {
if (i > 0) {
builder.append(" and ");
}
builder.append(dialect.wrapTableName(association.getJoinTableName())).append(".").append(joinTableInverseJoinColumnNames[i]).append("=")
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName())).append(".").append(dialect.wrapColumnName(joinTableInverseJoinReferencedColumnNames[i]));
}
} else {
// join column
builder.append(" left outer join ").append(dialect.wrapTableName(association.getObversePersistentEntity().getTableName())).append(" ")
.append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName()))
.append(" on ").append(quota(persistentEntity.getEntityName())).append(".").append(dialect.wrapColumnName(association.getJoinReferencedColumnName()))
.append("=").append(quota(persistentEntity.getEntityName() + "." + association.getInverse().getName())).append(".").append(dialect.wrapColumnName(association.getJoinColumnName()));
}
}

}
});

Expand Down
Loading