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 DTO projection with embedded id in JPA #2794

Merged
merged 2 commits into from
Feb 14, 2024
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 @@ -19,6 +19,8 @@ import io.micronaut.data.model.Pageable
import io.micronaut.data.tck.entities.AuthorBooksDto
import io.micronaut.data.tck.entities.Book
import io.micronaut.data.tck.entities.BookDto
import io.micronaut.data.tck.entities.Shipment
import io.micronaut.data.tck.entities.ShipmentId
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import org.hibernate.Hibernate
import spock.lang.Shared
Expand All @@ -33,10 +35,15 @@ class DtoSpec extends Specification {
@Inject
@Shared
BookRepository bookRepository

@Inject
@Shared
BookDtoRepository bookDtoRepository

@Inject
@Shared
JpaShipmentRepository shipmentRepository

def setup() {
bookRepository.saveAuthorBooks([
new AuthorBooksDto("Stephen King", Arrays.asList(
Expand Down Expand Up @@ -95,4 +102,26 @@ class DtoSpec extends Specification {
result.content.every { it.title.startsWith("The")}
}

void "test dto projection with embedded id"() {
given:
def id = new ShipmentId("a", "b")
shipmentRepository.save(new Shipment(id, "test"))

def id2 = new ShipmentId("a", "c")
shipmentRepository.save(new Shipment(id2, "test2"))

def id3 = new ShipmentId("b", "d")
shipmentRepository.save(new Shipment(id3, "test3"))
when:
def shipments = shipmentRepository.findAllByShipmentIdCountry("a")
def shipmentDtos = shipmentRepository.queryAllByShipmentIdCountry("a")
then:
shipmentDtos.size() == 2
shipmentDtos.every{ it.shipmentId()}
shipmentDtos.every{ it.shipmentId().country == "a"}
shipments.size() == 2
cleanup:
shipmentRepository.deleteAll()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
import io.micronaut.data.tck.entities.Shipment;
import io.micronaut.data.tck.entities.ShipmentDto;
import io.micronaut.data.tck.entities.ShipmentId;

import java.util.List;

@Repository
public interface JpaShipmentRepository extends CrudRepository<Shipment, ShipmentId> {

Expand All @@ -28,4 +31,8 @@ public interface JpaShipmentRepository extends CrudRepository<Shipment, Shipment
Shipment findByShipmentIdCountryAndShipmentIdCity(String country, String city);

long countDistinct();

List<Shipment> findAllByShipmentIdCountry(String country);

List<ShipmentDto> queryAllByShipmentIdCountry(String country);
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ protected void appendCompoundAssociationProjection(QueryState queryState, String
queryString.append(joinAlias).append(AS_CLAUSE).append(alias != null ? alias : association.getName());
}

@Override
protected void appendCompoundPropertyProjection(QueryState queryState, StringBuilder queryString, PersistentProperty property, PersistentPropertyPath propertyPath, String columnAlias) {
if (property instanceof Embedded) {
queryString.append(queryState.getRootAlias()).append(DOT).append(propertyPath.getPath());
if (columnAlias != null) {
queryString.append(AS_CLAUSE).append(columnAlias);
}
return;
}
super.appendCompoundPropertyProjection(queryState, queryString, property, propertyPath, columnAlias);
}

@Override
protected NamingStrategy getNamingStrategy(PersistentEntity entity) {
return JPA_NAMING_STRATEGY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import io.micronaut.data.model.PersistentEntity
import io.micronaut.data.model.entities.Person
import io.micronaut.data.model.query.builder.jpa.JpaQueryBuilder

import static io.micronaut.data.processor.visitors.TestUtils.getQuery

class DtoSpec extends AbstractDataSpec {

void "test build DTO with raw @Query method doesn't fail to compile"() {
Expand Down Expand Up @@ -403,4 +405,24 @@ class AuthorDto {
listAllMethod.isTrue(DataMethod, DataMethod.META_MEMBER_DTO)
}

void "test embedded id in DTO"() {
given:
def repository = buildRepository('test.TestRepository', '''
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.GenericRepository;
import io.micronaut.data.tck.entities.Shipment;
import io.micronaut.data.tck.entities.ShipmentDto;
import io.micronaut.data.tck.entities.ShipmentId;
@Repository
interface TestRepository extends GenericRepository<Shipment, ShipmentId> {
List<ShipmentDto> searchByShipmentIdCountry(String country);
}
''')
expect:"The repository to compile"
repository != null
when:
def queryFindByText = getQuery(repository.getRequiredMethod("searchByShipmentIdCountry", String))
then:
queryFindByText == 'SELECT shipment_.shipmentId AS shipmentId,shipment_.field AS field FROM io.micronaut.data.tck.entities.Shipment AS shipment_ WHERE (shipment_.shipmentId.country = :p1)'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.tck.entities;

import io.micronaut.core.annotation.Introspected;

@Introspected
public record ShipmentDto(

ShipmentId shipmentId,

String field
) {
}
Loading