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

Missing feature related to Class-based Projections. #2635

Closed
ogunodabass opened this issue Sep 19, 2022 · 5 comments
Closed

Missing feature related to Class-based Projections. #2635

ogunodabass opened this issue Sep 19, 2022 · 5 comments
Assignees
Labels
status: superseded An issue that has been superseded by another

Comments

@ogunodabass
Copy link

ogunodabass commented Sep 19, 2022

Hello Spring Team.

in the document: no nested projections can be applied
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections.dtos

I developed the code as application, did some tests and results. It is possible to query dynamically, without specifying the join type, lazy fields can join automatically without using join fetch or EntityGraph. Compared to the normal Entity class (even when querying the same fields), the speed of the select query is much faster.

@Data
public class UserDto1 {
	private Long id;
	private String email;
	private String password;
	private AdressDTO1 adressDTO;

	public UserDto1(Long id, String email, String password, Long adressId, int adressNo, String adressHouse) {
		this.id = id;
		this.email = email;
		this.password = password;
		this.adressDTO = new AdressDTO1(adressId, adressNo, adressHouse);
	}
}
@Data
@AllArgsConstructor
public class AdressDTO1 {

	private Long id;
	private int no;
	private String house;
}
public interface UserRepository extends JpaRepository<User, Long> {

	public <T> List<T> findBy(Class<T> clazz);
}
@Data
@Builder(setterPrefix = "set")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
@NoArgsConstructor


@Entity
@Table
public class User {

	@Id
	@SequenceGenerator(name = "user_sequence", sequenceName = "sq_user",allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
	private Long id;
	@Column
	private String email;
	@Column
	private String password;

	@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
	@ToString.Include(name = "id")
	private Adress adress;
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 19, 2022
gregturn added a commit that referenced this issue Sep 29, 2022
gregturn added a commit that referenced this issue Sep 29, 2022
gregturn added a commit that referenced this issue Sep 29, 2022
@gregturn
Copy link
Contributor

gregturn commented Mar 27, 2023

In light of #2814, have you checked out that this is now valid for @Query?

select new com.example.Dto(
    e.name,
    e.role,
    new com.example.Address(
        e.address.street,
        e.address.city,
        e.address.zipcode)
    )
from Employee e
join e.address 

This is a simple approximation of your query, but nevertheless it's legal in our HQL parser, and so should work with Hibernate, assuming you tune it to your specific DTO types.

@gregturn gregturn added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 27, 2023
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Apr 3, 2023
@ogunodabasss
Copy link

Very good. I tried something similar before, I can't remember what result I got.

However, in my benchmark tests, I discovered that DTO based queries are faster than entity and interface based projections. resultSetTransformer() gave the best results, but their performance was very similar. Interface-based projection uses too much System.gc(). If the System.gc problem is solved, it can perform the same as DTO based projection.

Additionally, the execution speed of queries for classes with the return type @ Entity is very slow.
Can development be done to speed it up?

Benchmark Result:
https://github.com/ogunodabass/Spring-JPA-Hibernate-Select-Query-Benchmark/tree/main/008DtoVsObjectVsProjectionVsTupleVsDtoMapper-Lazy-Speed/src/test/java/com/example/demo/benchmark/hot

BechhmarkGroupReturnEntity.findAll_User_JPA avgt 20 313996,003 ± 14203,533 us/op BechhmarkGroupReturnEntity.findBy_UserDto1_dynamic avgt 20 114260,209 ± 826,879 us/op BechhmarkGroupReturnEntity.findUserProjection_UserProjection_JPQL avgt 20 416818,290 ± 11266,894 us/op

Interface Based Projection GC Activity:
https://github.com/ogunodabass/Spring-JPA-Hibernate-Select-Query-Benchmark/blob/main/008DtoVsObjectVsProjectionVsTupleVsDtoMapper-Lazy-Speed/VisualVm%20Metrics/endpoint-ex22.png

visualVm test images of all the queries I compared in the application:
https://github.com/ogunodabass/Spring-JPA-Hibernate-Select-Query-Benchmark/blob/main/008DtoVsObjectVsProjectionVsTupleVsDtoMapper-Lazy-Speed/VisualVm%20Metrics/projection%20test-visualvm.png

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Apr 4, 2023
@gregturn
Copy link
Contributor

gregturn commented Aug 7, 2023

Given we now have first class support for DTO-based queries in both JPQL as well as HQL, I'm going to close this ticket.

If you wish to open a performance-based ticket, I encourage you to open a new ticket so we can tackle that as a separate effort.

@gregturn gregturn closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2023
@gregturn gregturn added status: superseded An issue that has been superseded by another and removed status: feedback-provided Feedback has been provided labels Aug 7, 2023
@gregturn
Copy link
Contributor

gregturn commented Aug 7, 2023

Superseded by #2814.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

4 participants