Skip to content
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

Fix incorrect multiple join query when selecting by entity identity #1272

Merged
merged 1 commit into from
Jan 19, 2022
Merged
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 @@ -1153,6 +1153,7 @@ private QueryPropertyPath findPropertyInternal(QueryState queryState, Persistent
}
PersistentProperty property = propertyPath.getProperty();
if (joinAssociation != null) {
// We don't need to join to access the id of the relation
if (property != joinAssociation.getAssociatedEntity().getIdentity()) {
String joinStringPath = joinPathJoiner.toString();
if (!queryState.isJoined(joinStringPath)) {
Expand All @@ -1161,13 +1162,14 @@ private QueryPropertyPath findPropertyInternal(QueryState queryState, Persistent
if (lastJoinAlias == null) {
lastJoinAlias = joinInPath(queryState, joinPathJoiner.toString());
}
}
if (lastJoinAlias != null) {
// 'joinPath.prop' should be represented as a path of 'prop' with a join alias
return new QueryPropertyPath(
new PersistentPropertyPath(Collections.emptyList(), property, property.getName()),
lastJoinAlias
);
}
// We don't need to join to access the id of the relation
}
} else if (TypeRole.ID.equals(name) && entity.getIdentity() != null) {
// special case handling for ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,28 @@ interface UserRoleRepository extends GenericRepository<UserRole, UserRoleId> {
getDataTypes(method) == [DataType.LONG]
}

void "test multiple join query by identity"() {
given:
def repository = buildRepository('test.CitiesRepository', """
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.tck.entities.City;
import java.util.UUID;

@JdbcRepository(dialect= Dialect.MYSQL)
@io.micronaut.context.annotation.Executable
interface CitiesRepository extends CrudRepository<City, Long> {

@Join("countryRegion")
@Join("countryRegion.country")
int countDistinctByCountryRegionCountryUuid(UUID id);
}
""")
def query = getQuery(repository.getRequiredMethod("countDistinctByCountryRegionCountryUuid", UUID))

expect:
query == 'SELECT COUNT(*) FROM `T_CITY` city_ INNER JOIN `CountryRegion` city_country_region_ ON city_.`country_region_id`=city_country_region_.`id` INNER JOIN `country` city_country_region_country_ ON city_country_region_.`countryId`=city_country_region_country_.`uuid` WHERE (city_country_region_country_.`uuid` = ?)'

}

}