JPA SQL Result Mapping

Tags: , , ,



my code is mapping query result to DTO using Sql Result Mapping and create list with these dtos but in my database id can be null and it gives me trouble in mapping. That’s why I don’t want to use it instead of that, is there any way to generate id’s for these dto’s not getting them from sql query? I can’t change my sql data, because it’s not a personal data.

My mapping code looks like this and fyi I did not use @ConstructorResult, I’m not sure what is the difference between @EntityResult and @ConstructorResult but in entity I’m giving column name and I have to match my table column with my DTO class.

@SqlResultSetMapping(
        name = "ApplicationMapping",
        entities = @EntityResult(entityClass = com.sqlresultsetmapping.ApplicationDto.class,
                fields = {
                        @FieldResult(name = "name", column = "name"),
                        @FieldResult(name = "create", column = "create"),
                        @FieldResult(name = "update", column = "update"),
                        @FieldResult(name = "version", column = "version"),
                        @FieldResult(name = "image", column = "image"),
                        @FieldResult(name = "id", column = "id")}))

Answer

You must use ConstructorResult because you want to have a DTO as the result.

EntityResult is for mapping SQL results to Entities.

If you use ConstructorResult the null in the ID column will not matter. But with EntityResult an Entity must have an ID.



Source: stackoverflow