Skip to content

3.5.0 throws exception when returning different entity from @Query #3862

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

Open
eryanv opened this issue Apr 29, 2025 · 1 comment
Open

3.5.0 throws exception when returning different entity from @Query #3862

eryanv opened this issue Apr 29, 2025 · 1 comment
Labels
status: waiting-for-triage An issue we've not yet triaged

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 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants