Skip to content

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

Closed
@eryanv

Description

@eryanv

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.

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions