SqlResultSetMapping未找到

huangapple go评论67阅读模式
英文:

SqlResultSetMapping not found

问题

I use spring boot 3. I try to create a native query with entity manager, i would like to feed a dto

@SqlResultSetMapping(
        name = "BookResultMapping",
        classes = @ConstructorResult(
                targetClass = BookSearchResult.class,
                columns = {
                        @ColumnResult(name = "code"),
                        @ColumnResult(name = "title")}))
public class BookSearchService {

    @PersistenceContext
    private EntityManager entityManager;

    public void bookSearch(Search search){
         StringBuilder sbSqlQuery = new StringBuilder();
         ...
         Query query = entityManager.createNativeQuery(sbSqlQuery.toString(), "BookResultMapping");
         ...
    }
}

@Data
@RequiredArgsConstructor
public class BookSearchResult {

    private String code;
    private String title;
}

When i run this code, I get

Could not resolve specified result-set mapping name : BookResultMapping

Edit, I tried to but code related to SqlResultSetMapping directly to the dto, but i get same result

英文:

I use spring boot 3. I try to create a native query with entity manager, i would like to feed a dto

@SqlResultSetMapping(
        name = "BookResultMapping",
        classes = @ConstructorResult(
                targetClass = BookSearchResult.class,
                columns = {
                        @ColumnResult(name = "code"),
                        @ColumnResult(name = "title")}))
public class BookSearchService {

    @PersistenceContext
    private EntityManager entityManager;

    public void bookSearch(Search search){
         StringBuilder sbSqlQuery = new StringBuilder();
         ...
         Query query = entityManager.createNativeQuery(sbSqlQuery.toString(), "BookResultMapping");
         ...
    }
}


@Data
@RequiredArgsConstructor
public class BookSearchResult {

    private String code;
    private String title;
}

When i run this code, I get

> Could not resolve specified result-set mapping name : BookResultMapping

Edit, I tried to but code related to SqlResultSetMapping directly to the dto, but i get same result

答案1

得分: 1

请查看这个其他答案:https://stackoverflow.com/a/36109273

简而言之,@SqlResultSetMapping 注解应该放在你的 @Entity 类中的一个以便被检测到。我认为这个文档编写得不够好,在线示例总是展示孤立的注解,缺乏上下文信息。

如果你想避免使用任何实体,你可以像这样做:

@SqlResultSetMapping(
    name = "MessageDTO",
    classes = @ConstructorResult(targetClass = MessageDTO.class, columns = {
        @ColumnResult(name = "message_body", type = String.class),
        @ColumnResult(name = "created_date", type = OffsetDateTime.class)
    })
)
@Embeddable
public static class SQLMappingConfigEntity {
    /* 这个虚拟的 Embeddable 类允许 SqlResultSetMapping 注解被识别 */
}

在这个示例中,MessageDTO 将是你的 DTO。它必须有一个包含所有参数的构造函数。

英文:

Check this other answer: https://stackoverflow.com/a/36109273

In short, the @SqlResultSetMapping annotation should go in one of your @Entity classes in order to be detected. I think this is poorly documented and online examples always show the isolated annotation without much context.

If you wanted to avoid using any entity, you can so something like:

@SqlResultSetMapping(
		name = "MessageDTO",
		classes = @ConstructorResult(targetClass = MessageDTO.class, columns = {
				@ColumnResult(name = "message_body", type = String.class),
				@ColumnResult(name = "created_date", type = OffsetDateTime.class)
		})
)
@Embeddable
public static class SQLMappingConfigEntity {
	/* this dummy Embeddable class allows SqlResultSetMapping annotation to be picked up */
}

In this example, MessageDTO would be your DTO. It must have with a constructor will all arguments.

huangapple
  • 本文由 发表于 2023年6月13日 05:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定