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

Contributing to Spring Data JPA Documentation #3755

Closed
ogunodabasss opened this issue Jan 28, 2025 · 2 comments
Closed

Contributing to Spring Data JPA Documentation #3755

ogunodabasss opened this issue Jan 28, 2025 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@ogunodabasss
Copy link

ogunodabasss commented Jan 28, 2025

I want to add a feature that is not published by the documentation.
Class Based Projection

This feature is only valid for JPA queries. We can choose the columns we want according to the DTO model. An alternative to interface based projection.

If you want to check it out, I share the medium and github links:
medium
github

In code, the feature is as follows:

Entity User:
`public class User {
@id
@SequenceGenerator(name = "user_sequence", sequenceName = "sq_user",allocationSize = 1_000)
@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;
/*
@OverRide
public String toString() {
return "User [id=" + id + ", email=" + email + ", password=" + password
+ ", adress=" + this.getAdress().getId()
+ "]";
}
*/
}`

Entity Adress:
`public class Adress {
@ToString.Include
@EqualsAndHashCode.Include
@id
@SequenceGenerator(name = "adres_sequence", sequenceName = "sq_adres",allocationSize = 1_000)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "adres_sequence")
private Long id;
@column
private Integer no;
@column
private String house;

public Adress(Long id) {
this.id = id;
}
}`

DTO AdressDTO1:
`@Data
@AllArgsConstructor
public class AdressDTO1 {

private Long id;
private int no;
private String house;
}`

DTO UserDto1:
`@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);
}
}`

`//Repository Class Method:
public List findBy(Class clazz);

//Service Layer Method:
userRepository.findBy(UserDto1.class);
`

Select Query:
select user0_.id as col_0_0_, user0_.email as col_1_0_, user0_.password as col_2_0_, adress1_.id as col_3_0_, adress1_.no as col_4_0_, adress1_.house as col_5_0_ from user user0_ left outer join adress adress1_ on user0_.adress_id=adress1_.id

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 28, 2025
@christophstrobl
Copy link
Member

If you would like us to spend some time helping you with the issue at hand, please spend some time describing it. Provide proper formatted code, links that don't return 404, etc. and tell us what you want to achieve.

@christophstrobl christophstrobl 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 Jan 28, 2025
@christophstrobl
Copy link
Member

Duplicate of #3754

@christophstrobl christophstrobl marked this as a duplicate of #3754 Jan 28, 2025
@christophstrobl christophstrobl added status: duplicate A duplicate of another issue and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants