首页 > 解决方案 > 我如何告诉 SpringData/Hibernate 我想要我的非唯一结果作为 java 集合?

问题描述

所以我正在开发一个支持本地化的应用程序,所以我的数据库有本地化。我有这个查询(使用@Query 注释):

select new com.fullstack.daos.projections.LocalizedTutorialDAO(t.id, t.created, t.createdBy, t.lastModified, t.lastModifiedBy, t.version, t.exclusive, (VALUE(l)).name, (VALUE(l)).description, tp) from tutorial t join t.localizations l join t.topics tp where (VALUE(l)).name like %:name% and (KEY(l)) = :lang

如果我不包括 t.topics 它工作得很好,但如果我这样做,它会给我:

Caused by: javax.persistence.NonUniqueResultException: query did not return a unique result: 3

事情是,这应该发生,看到 t.topics 是一组代表 @ElementCollection 的字符串。当我执行默认的 findById 时,它会获取它,为什么在这种情况下我的投影不能将所谓的“非唯一结果”转换为它应该是的 Set?

public class LocalizedTutorialDAO {

    private UUID id;
    private LocalDateTime created;
    private String createdBy;
    private LocalDateTime lastModified;
    private String lastModifiedBy;
    private int version;
    private String name, description;
    private Set<String> topics = new HashSet<>();

    public LocalizedTutorialDAO(UUID id, LocalDateTime created, String createdBy, LocalDateTime lastModified,
            String lastModifiedBy, int version, boolean exclusive, String name, String description, String topics) {
        super(id, created, createdBy, lastModified, lastModifiedBy, version);
        this.name = name;
        this.description = description;
        this.topics = topics;
    }
}

标签: javaspring-boothibernatespring-data-jpa

解决方案


推荐阅读