given the entity model below:
@Entity(name = "Accounts")
open class AccountEntity(
@field:Id
@field:GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accounts_sequence_generator")
@field:SequenceGenerator(name = "accounts_sequence_generator", sequenceName = "sequence_accounts")
@field:Column(name = "id", nullable = false, unique = true)
open var id: Long? = null,
@field:[NotBlank Size(min = 2, max = 255, message = "username must be between 2 and 255 characters long")]
@field:Column(name = "username", nullable = false, unique = true)
open var username: String,
@field:Embedded
open var address: Address?
)
@Embeddable
data class Address(
@field:Embedded
val geolocation: Geolocation
)
@Embeddable
data class Geolocation(
@field:Column(name = "geolocation", columnDefinition = "geography(POINT,4326)")
val geolocation: Point
)
I would like to execute a query using a DTO projection with a constructor expression:
val query = entityManager.createQuery(
"select new org.example.dto.AccountSummary(acc.address.geolocation.geolocation, acc.id, acc.username) from Accounts acc" +
"",
AccountSummary::class.java
)
return query.resultList
where the AccountSummary class is given below:
@Introspected
data class AccountSummary(
val point: Location,
val id: Long,
val username: String
)
However, I would also like to perform a type conversion on a geolocation property (type Point) to a custom Location data class, so I've registered a custom TypeConverter from a Geometry to Location:
@Singleton
class GeometryLocationConverter : TypeConverter<Geometry, Location> {
override fun convert(`object`: Geometry?, targetType: Class<Location>?, context: ConversionContext?): Optional<Location> {
return when (`object`) {
is Point -> Optional.of(Location(`object`.y, `object`.x))
else -> throw Exception("unknown geometry type")
}
}
}
However, an exception gets thrown with the error: unable to locate appropriate constructor on class AccountSummary. Is something like this possible? I haven't found any examples that showcase this use case.