英文:
JPA SQL Result Mapping
问题
我的代码正在使用Sql结果映射将查询结果映射到DTO,并使用这些DTO创建列表,但在我的数据库中,id可以为null
,这在映射中给我带来了麻烦。这就是为什么我不想使用它的原因,有没有办法为这些DTO生成id,而不是从SQL查询中获取它们?我无法更改我的SQL数据,因为它不是个人数据。
我的映射代码如下,提醒一下,我没有使用@ConstructorResult
,我不确定@EntityResult
和@ConstructorResult
之间的区别,但在实体中,我正在指定列名,我必须将我的表列与DTO类匹配。
@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")
})
)
英文:
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")}))
答案1
得分: 1
你必须使用ConstructorResult,因为你希望将DTO作为结果。
EntityResult用于将SQL结果映射到实体。
如果你使用ConstructorResult,ID列中的null值将不重要。但使用EntityResult,实体必须具有ID。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论