Skip to content

DTO Projection rewriting rewrites queries returning properties to DTO form #3862

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
eryanv opened this issue Apr 29, 2025 · 2 comments
Closed
Assignees
Labels
type: regression A regression from a previous release

Comments

@eryanv
Copy link

eryanv commented Apr 29, 2025

The 3.5.0 release line throws a org.hibernate.query.SyntaxException when a @query tries to return a property of an object which itself is a different entity. A basic example of the entity relationships and repository method are shown below. This code attached works as expected under 3.4.5, where the country property of the entity is returned for use.

@Entity
public class Country {
	public Country(Integer id, String isoCode, String name) { this.id = id; this.iso3Code = isoCode; this.name = name; }
	public Country() {	}
	
	@Id
	private Integer id;
	
	@Column(length=3)
	private String iso3Code;
	
	@Column(length=50)
	private String name;
        // Getters/Setters excluded
}

@Entity
public class GroupCountryMapping {
	public GroupCountryMapping(String id, String name, Country country) { this.id = id; this.name = name;		this.country = country; }
	public GroupCountryMapping() { }

	@Id
	@Column(length=2)
	String id;
	
	@Column(length=20)
	String name;
	
	@ManyToOne
	@JoinColumn(name="country_id", nullable = false, foreignKey=@ForeignKey(name = "Fk_gcm_country"))
	Country country;
        // Getters/Setters excluded
}

@Repository
public interface GroupCountryMappingRepository extends JpaRepository<GroupCountryMapping,String> {
	@Query("SELECT country FROM GroupCountryMapping WHERE id=:grp")
	Optional<Country> getCountryByGroupId(String grp);
}

If the query string is updated to SELECT gcm.country FROM GroupCountryMapping gcm WHERE gcm.id=:grp it appears that spring data/hibernate looks for a copy constructor rather than the typical no argument constructor.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 29, 2025
@eryanv
Copy link
Author

eryanv commented Apr 29, 2025

Github doesn't seem want to let me upload a sample project, so hopefully this link to the repo:
https://github.com/eryanv/springdata-demo

@mp911de mp911de self-assigned this May 5, 2025
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 5, 2025
@mp911de mp911de changed the title 3.5.0 throws exception when returning different entity from @Query DTO Projection rewriting rewrites queries returning properties to DTO form May 5, 2025
@mp911de mp911de added this to the 3.5 GA (2025.0.0) milestone May 5, 2025
@mp911de mp911de added type: regression A regression from a previous release and removed type: bug A general bug labels May 5, 2025
@mp911de
Copy link
Member

mp911de commented May 5, 2025

Thanks a lot for exploring our release candidate. That is a regression introduced with DTO projection rewriting.

We're now introspecting the query closer for projections and property paths.

@mp911de mp911de closed this as completed in d463f5f May 5, 2025
mp911de added a commit that referenced this issue May 5, 2025
…operties.

We back off from rewriting String-based queries to use DTO Constructor expressions if the query selects a property that is assignable to the return type.

Closes #3862
mp911de added a commit that referenced this issue May 13, 2025
…operties.

We back off from rewriting String-based queries to use DTO Constructor expressions if the query selects a property that is assignable to the return type.

Closes #3862
mp911de added a commit that referenced this issue May 15, 2025
…operties.

We back off from rewriting String-based queries to use DTO Constructor expressions if the query selects a property that is assignable to the return type.

Closes #3862
mp911de added a commit that referenced this issue May 16, 2025
…operties.

We back off from rewriting String-based queries to use DTO Constructor expressions if the query selects a property that is assignable to the return type.

Closes #3862
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

3 participants