Skip to content

Commit

Permalink
quarkusio#36581 Simplify test, apply @loicmatthieu comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
humcqc committed Nov 28, 2023
1 parent 5c555d8 commit 16db411
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 130 deletions.
14 changes: 7 additions & 7 deletions docs/src/main/asciidoc/hibernate-orm-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -939,31 +939,31 @@ PanacheQuery<DogDto> query = Dog.findAll().project(DogDto.class);
----
<1> The `ownerName` DTO constructor's parameter will be loaded from the `owner.name` HQL property.

In case you want to project nested class like Person in Dog entity you can use `@NestedProjectedClass` annotation on projected class.
In case you want to project an entity in a class with nested classes, you can use the `@NestedProjectedClass` annotation on those nested classes.

[source,java]
----
@RegisterForReflection
public class DogDto2 {
public class DogDto {
public String name;
public PersonDto2 owner;
public PersonDto owner;
public DogDto2(String name, PersonDto2 owner) {
public DogDto(String name, PersonDto owner) {
this.name = name;
this.owner = owner;
}
@NestedProjectedClass // <1>
public static class PersonDto2 {
public static class PersonDto {
public String name;
public PersonDto2(String name) {
public PersonDto(String name) {
this.name = name;
}
}
}
PanacheQuery<DogDto2> query = Dog.findAll().project(DogDto2.class);
PanacheQuery<DogDto> query = Dog.findAll().project(DogDto.class);
----

<1> This annotation can be used when you want to project `@Embedded` entity or `@ManyToOne`, `@OneToOne` relation.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/hibernate-reactive-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ PanacheQuery<DogDto> query = Dog.findAll().project(DogDto.class);
----
<1> The `ownerName` DTO constructor's parameter will be loaded from the `owner.name` HQL property.

In case you want to project nested class like Person in Dog entity you can use `@NestedProjectedClass` annotation on projected class.
In case you want to project an entity in a class with nested classes, you can use the `@NestedProjectedClass` annotation on those nested classes.

[source,java]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.lang.annotation.Target;

/**
* Define a Class that is used for query projection,
* Identify nested object type that is projected.
* Define a class that is used for query projection as a nest inside the projected class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.lang.annotation.Target;

/**
* Define a Class that is used for query projection,
* Identify nested object type that is projected.
* Define a class that is used for query projection as a nest inside the projected class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,36 @@
public class PersonDTO extends PersonName {

public final AddressDTO address;
@ProjectedFieldName("address")
public final AddressDTO address2;

public final DescriptionDTO description;

@ProjectedFieldName("description.size")
public final Integer directHeight;

public PersonDTO(String uniqueName, String name, AddressDTO address, AddressDTO address2, DescriptionDTO description,
public PersonDTO(String uniqueName, String name, AddressDTO address, DescriptionDTO description,
Integer directHeight) {
super(uniqueName, name);
this.address = address;
this.address2 = address2;
this.description = description;
this.directHeight = directHeight;
}

@NestedProjectedClass
public static class AddressDTO implements Comparable<AddressDTO> {

// Simple filed with automatic mapping in constructor
// Simple field with automatic mapping in constructor
public final String street;

@ProjectedFieldName("street")
public final String street2;

private final String street3;

public AddressDTO(String street, String street2, @ProjectedFieldName("street") String street3) {
public AddressDTO(String street) {
this.street = street;
this.street2 = street2;
this.street3 = street3;

}

@Override
public int compareTo(AddressDTO address) {
return street.compareTo(address.street);
}

public String getStreet3() {
return street3;
}
}

@NestedProjectedClass
Expand All @@ -59,27 +47,13 @@ public static class DescriptionDTO {
@ProjectedFieldName("size")
public final Integer height;

public final EmbeddedDescriptionDTO description2;

public DescriptionDTO(Integer height, Integer weight, EmbeddedDescriptionDTO description2) {
public DescriptionDTO(Integer height, Integer weight) {
this.height = height;
this.description2 = description2;
this.description = "Height: " + height + ", weight: " + weight;
}

public String getGeneratedDescription() {
public String getDescription() {
return description;
}
}

@NestedProjectedClass
public static class EmbeddedDescriptionDTO {
@ProjectedFieldName("embeddedDescription")
public final String value;

public EmbeddedDescriptionDTO(String value) {
this.value = value;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
public class PersonDescription {
public Integer size;
public Integer weight;
public PersonDescriptionEmbedded description2;

}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void testEnhancement27184DeleteDetached() {
void testSimpleEntityProjection() {
Person person = new Person();
person.name = "1";
person.uniqueName = "1";
person.persist();

PersonName personName = Person.find(" name = ?1", "1")
Expand All @@ -261,7 +262,6 @@ void testSimpleEntityProjection2() {
person.name = "1";
person.uniqueName = "1";
person.persist();

String personName = Person.find("select name from Person2 where name = ?1", "1")
.project(String.class)
.firstResult();
Expand All @@ -274,24 +274,21 @@ void testSimpleEntityProjection2() {
void testNestedEntityProjection_WithQuery() {
Person person = new Person();
person.name = "2";
person.uniqueName = "2";
person.address = new Address("street 2");
person.persist();
PersonDTO personNameAddress = Person.find(
PersonDTO personDTO = Person.find(
"select uniqueName, name, " +
" new io.quarkus.it.panache.PersonDTO$AddressDTO( address.street, address.street, address.street)," +
" new io.quarkus.it.panache.PersonDTO$AddressDTO( address.street, address.street, address.street)," +
" new io.quarkus.it.panache.PersonDTO$DescriptionDTO(description.size, description.weight, " +
" new io.quarkus.it.panache.PersonDTO$EmbeddedDescriptionDTO(description.description2.embeddedDescription)"
+
")," +
" new io.quarkus.it.panache.PersonDTO$AddressDTO(address.street)," +
" new io.quarkus.it.panache.PersonDTO$DescriptionDTO(description.size, description.weight)," +
" description.size" +
" from Person2 where name = ?1",
"2")
.project(PersonDTO.class)
.firstResult();
person.delete();
Assertions.assertEquals("2", personNameAddress.name);
Assertions.assertEquals("street 2", personNameAddress.address2.street);
Assertions.assertEquals("2", personDTO.name);
Assertions.assertEquals("street 2", personDTO.address.street);
}

@Test
Expand All @@ -304,33 +301,28 @@ void testNestedEntityProjection() {
person.description = new PersonDescription();
person.description.weight = 75;
person.description.size = 170;
person.description.description2 = new PersonDescriptionEmbedded();
person.description.description2.embeddedDescription = "embedded";
person.persist();
PersonDTO personDTO = Person.find(" name = ?1", "3")
.project(PersonDTO.class)
.firstResult();
person.delete();
Assertions.assertEquals("3", personDTO.name);
Assertions.assertEquals("street 3", personDTO.address2.street);
Assertions.assertEquals("street 3", personDTO.address2.street2);
Assertions.assertEquals("street 3", personDTO.address2.getStreet3());
Assertions.assertEquals("street 3", personDTO.address.street);
Assertions.assertEquals(170, personDTO.directHeight);
Assertions.assertEquals(170, personDTO.description.height);
Assertions.assertEquals("Height: 170, weight: 75", personDTO.description.getGeneratedDescription());
Assertions.assertEquals("embedded", personDTO.description.description2.value);
Assertions.assertEquals("Height: 170, weight: 75", personDTO.description.getDescription());
}

@Test
@Transactional
void testDogDto2Projection() {
Person hum = new Person();
hum.name = "hum";
hum.uniqueName = "hum";
Dog kit = new Dog("kit", "bulldog");
hum.dogs.add(kit);
kit.owner = hum;
hum.persist();

DogDto2 dogDto2 = Dog.find(" name = ?1", "kit")
.project(DogDto2.class)
.firstResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class DogDto2 {
public class DogDto {
public String name;
public PersonDto2 owner;
public PersonDto owner;

public DogDto2(String name, PersonDto2 owner) {
public DogDto(String name, PersonDto owner) {
this.name = name;
this.owner = owner;
}

@NestedProjectedClass
public static class PersonDto2 {
public static class PersonDto {
public String name;

public PersonDto2(String name) {
public PersonDto(String name) {
this.name = name;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.*;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Transient;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlTransient;

Expand Down Expand Up @@ -36,11 +47,8 @@
})
@FilterDef(name = "Person.hasName", defaultCondition = "name = :name", parameters = @ParamDef(name = "name", type = String.class))
@FilterDef(name = "Person.isAlive", defaultCondition = "status = 'LIVING'")
@FilterDef(name = "Person.name.in", defaultCondition = "name in (:names)", parameters = {
@ParamDef(name = "names", type = String.class) })
@Filter(name = "Person.isAlive")
@Filter(name = "Person.hasName")
@Filter(name = "Person.name.in")
public class Person extends PanacheEntity {

public String name;
Expand Down Expand Up @@ -88,8 +96,4 @@ public void setSerialisationTrick(int serialisationTrick) {
public static long methodWithPrimitiveParams(boolean b, byte bb, short s, int i, long l, float f, double d, char c) {
return 0;
}

public static void voidMethod() {
throw new RuntimeException("void");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,35 @@

@RegisterForReflection
public class PersonDTO extends PersonName {

public final AddressDTO address;
@ProjectedFieldName("address")
public final AddressDTO address2;

public final DescriptionDTO description;

@ProjectedFieldName("description.size")
public final Integer directHeight;

public PersonDTO(String uniqueName, String name, AddressDTO address, AddressDTO address2, DescriptionDTO description,
public PersonDTO(String uniqueName, String name, AddressDTO address, DescriptionDTO description,
Integer directHeight) {
super(uniqueName, name);
this.address = address;
this.address2 = address2;
this.description = description;
this.directHeight = directHeight;
}

@NestedProjectedClass
public static class AddressDTO implements Comparable<AddressDTO> {

// Simple field with automatic mapping in constructor
public final String street;

@ProjectedFieldName("street")
public final String street2;

private final String street3;

public AddressDTO(String street, String street2, @ProjectedFieldName("street") String street3) {
public AddressDTO(String street) {
this.street = street;
this.street2 = street2;
this.street3 = street3;
}

@Override
public int compareTo(AddressDTO address) {
return street.compareTo(address.street);
}

public String getStreet3() {
return street3;
}
}

@NestedProjectedClass
Expand All @@ -59,27 +44,13 @@ public static class DescriptionDTO {
@ProjectedFieldName("size")
public final Integer height;

public final EmbeddedDescriptionDTO description2;

public DescriptionDTO(Integer height, Integer weight, EmbeddedDescriptionDTO description2) {
public DescriptionDTO(Integer height, Integer weight) {
this.height = height;
this.description2 = description2;
this.description = "Height: " + height + ", weight: " + weight;
}

public String getGeneratedDescription() {
return description;
}
}

@NestedProjectedClass
public static class EmbeddedDescriptionDTO {
@ProjectedFieldName("embeddedDescription")
public final String value;

public EmbeddedDescriptionDTO(String value) {
this.value = value;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
public class PersonDescription {
public Integer size;
public Integer weight;
public PersonDescriptionEmbedded description2;

}
Loading

0 comments on commit 16db411

Please sign in to comment.