question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Panache Query/DTO projection with fields from referenced entity

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
loicmathieucommented, Feb 22, 2021

@anthony-quiros please open a different github issue for your example, did you try your example ? Which exception did it throws ?

0reactions
FroMagecommented, Mar 8, 2021

That feature sounds very reasonable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Panache Query/DTO projection with fields ... - Google Groups
Hi all,. currently, the Panache projection works great if the parameters in the DTO's constructor belongs to the same entity. The case of...
Read more >
How to select only certain fields with Quarkus Panache?
All the guides explain how to write different queries, but is not clear how to select only certain attributes. If I don't need...
Read more >
ProjectedFieldName (Quarkus - Hibernate ORM with Panache
Define a field's path for the SELECT statement when using a projection DTO. It supports the "dot" notation for fields in referenced entities:...
Read more >
Simplified MongoDB with Panache - Quarkus
This POJO needs to be annotated with @ProjectionFor(Entity.class) where Entity is the name of your entity class. The field names, or getters, of...
Read more >
Why, When and How to Use DTO Projections with JPA and ...
Here you can see an example of a query that returns a list of BookWithAuthorNames object. I provide the fully qualified class name,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found