Panache Query/DTO projection with fields from referenced entity
See original GitHub issueDescription
Currently, the Panache project
method works great if the parameters in the DTO’s constructor belongs to the same entity.
The case of having a DTO with also fields from referenced entities is not working because the required “dot” notation to build a working query is not compatible with Java naming that doesn’t allow the usage of dots .
in a parameter’s name.
Example DogDto
based on Person
and Dog
entities from hibernate-orm-panache
integration tests
@Entity
public class Person extends PanacheEntity {
public String name;
@Column(unique = true)
public String uniqueName;
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public List<Dog> dogs = new ArrayList<>();
}
@Entity
public class Dog extends PanacheEntityBase {
@Id
@GeneratedValue
public Integer id;
public String name;
public String race;
@ManyToOne
public Person owner;
}
@RegisterForReflection
public class DogDto {
public String name;
public String ownerName;
public DogDto(String name, String ownerName) {
this.name = name;
this.ownerName = ownerName;
}
}
The DogDto
’s constructor parameters have no way to reference the field name
from the entity Person
Implementation ideas
The idea is to add a ProjectionForField
annotation for ElementType.PARAMETER
to let the developers provide the “SQL path” to get the value for the field the annotation refers to in the DTO’s constructor, considering the projection works starting from the DTO’s constructor parameters.
Example based on the above use case
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface ProjectionForField {
String value();
}
@RegisterForReflection
public class DogDto {
public String name;
public String ownerName;
public DogDto(String name, @ProjectionForField("owner.name") String ownerName) {
this.name = name;
this.ownerName = ownerName;
}
}
The annotation’s name ProjectionForField
wants to be consistent with the annotation ProjectionFor
(available for Panache Mongo extension) that refers to classes.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
@anthony-quiros please open a different github issue for your example, did you try your example ? Which exception did it throws ?
That feature sounds very reasonable.