java—尝试使用spring数据检索elementcollection时出现sql错误

iqxoj9l9  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(223)

我有一个实体叫做 AffiliateLink 有一个名为 keywords . 我正在尝试从中检索字段的子集 AffiliateLink 这包括 keywords 现场。为此,我在我的生活中采用了这种方法 AffiliateLinkDao .

@Query("SELECT new DTOs.EntityDTOs.AffiliateLinkUpdateDTO(s.affiliateUrl, s.title, s.description, s.productValue, s.general, s.rank, s.seedId, s.plantClimbs, s.spicy, s.teaPlant, s.keywords) FROM AffiliateLink s WHERE s.id = :id")
public AffiliateLinkUpdateDTO getAffiliateLinkDetailsById(@Param("id") long id);

如您所见,我正在使用一个名为 AffiliateLinkUpdateDTO 获取所需字段。此对象扩展了另一个名为 AffiliateLinkCreateDTO 因为它们共享相似的字段,所以sql语法错误是由s.keywords引起的,错误消息如下:
com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception:您的sql语法有错误;查看与您的mysql服务器版本对应的手册,以获得正确的语法,使用第1行的near'as col\u 10\u 0\ufrom affiliatelink affiliatel0\uuu inner join affiliate\u keywords keywo'
出于某种原因,它试图做一个 inner joinaffiliate_keywords keywoCollection table 被命名为 affiliate_keywords . 我不知道为什么它会这样做,我想知道是否有人能给我一些关于我可能做错了什么导致这个错误的建议。以下是使用的类:
这个 AffiliateLink 班级:

@Entity
public class AffiliateLink implements Serializable {
    @Id
    @GeneratedValue(generator = "ID_GENERATOR")
    private Long id;

    @URL
    private String affiliateUrl;

    @URL
    private String affiliateImageUrl;

    private String title;

    private String description;

    private Double productValue;

    private boolean general;

    private byte rank;

    private boolean linkBroken;

    @NotNull
    private Boolean plantClimbs;

    @NotNull
    private Boolean spicy;

    @NotNull
    private Boolean teaPlant;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(updatable = false)
    @CreationTimestamp
    private Date creationDate;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "affiliate_keywords", joinColumns = @JoinColumn(name = "affiliate_id"))
    @Column(name = "keywords")
    private Set<String> keywords;

    @ManyToMany(mappedBy = "affiliateLinks")
    private Set<SeedRecord> seedRecords = new HashSet<>();

    @Enumerated(EnumType.STRING)
    private LocalizedStorefront localizedStorefront;

    private long seedId;

这个 AffiliateLinkUpdateDTO 班级:

public class AffiliateLinkUpdateDTO extends AffiliateLinkCreateDTO {
    @NotNull
    private long affiliateId;

    private String affiliateImageUrl;

    public AffiliateLinkUpdateDTO() {
    }

    public AffiliateLinkUpdateDTO(String affiliateUrl, String title, String description, Double productValue, Boolean general, Byte rank, Long seedId, Boolean plantClimbs,
                              Boolean spicy, Boolean teaPlant, Set<String> keywords){
        super(affiliateUrl, title, description, productValue, general, rank, seedId, plantClimbs, spicy, teaPlant, keywords);
        this.affiliateId = affiliateId;
        this.affiliateImageUrl = affiliateImageUrl;
    }

这个 AffiliateLinkCreateDTO 由updatedto类扩展的类

public class AffiliateLinkCreateDTO {

    @NotNull
    @NotEmpty
    @NotBlank
    @Pattern(regexp = "http?://.+")
    private String affiliateUrl;

    @NotNull
    @NotEmpty
    @NotBlank
    @Size(min=2, max=14)
    private String title;
    @NotNull
    @NotEmpty
    @NotBlank
    @Size(min=2, max=130)
    private String description;

    @NotNull
    private Double productValue;

    private String base64Hash;

    private String imageType;

    @NotNull
    private Boolean general;

    private Byte rank;

    private Long seedId;

    @NotNull
    private Boolean plantClimbs;

    @NotNull
    private Boolean spicy;

    @NotNull
    private Boolean teaPlant;

    private Set<String> keywords = new HashSet<>(); 

    @NotNull
    @NotEmpty
    @NotBlank
    private LocalizedStorefront localizedStorefront;

    public AffiliateLinkCreateDTO() {
    }

    public AffiliateLinkCreateDTO(String affiliateUrl, String title, String description, Double productValue, Boolean general, Byte rank, Long seedId, Boolean plantClimbs, Boolean spicy, Boolean teaPlant, Set<String> keywords) {
        this.affiliateUrl = affiliateUrl;
        this.title = title;
        this.description = description;
        this.productValue = productValue;
        this.general = general;
        this.rank = rank;
        this.seedId = seedId;
        this.plantClimbs = plantClimbs;
        this.spicy = spicy;
        this.teaPlant = teaPlant;
        this.keywords = keywords;
        this.localizedStorefront = localizedStorefront;
    }

这是答案 @Query 我正在使用的检索方法:

@Repository
public interface AffiliateLinkDao extends JpaRepository<AffiliateLink, Long> {

    @Query("SELECT new DTOs.EntityDTOs.AffiliateLinkUpdateDTO(s.affiliateUrl, s.title, s.description, s.productValue, s.general, s.rank, s.seedId, s.plantClimbs, s.spicy, s.teaPlant, s.keywords) FROM AffiliateLink s WHERE s.id = :id")
    public AffiliateLinkUpdateDTO getAffiliateLinkDetailsById(@Param("id") long id);

}

编辑:
当我把构造器简化为s.keywords时,比如:

@Query("SELECT new DTOs.EntityDTOs.AffiliateLinkUpdateDTO(s.keywords) FROM AffiliateLink s WHERE id = :id")
public AffiliateLinkUpdateDTO getAffiliateLinkDetailsById(@Param("id") long id);

我还是会犯同样奇怪的错误
编辑:
导致错误的完全生成的sql命令是:

2018-08-02 13:27:12.438 DEBUG 1228 --- [nio-8080-exec-2] org.hibernate.SQL                        : select affiliatel0_.affiliateUrl as col_0_0_, affiliatel0_.title as col_1_0_, affiliatel0_.description as col_2_0_, affiliatel0_.productValue as col_3_0_, affiliatel0_.general as col_4_0_, affiliatel0_.rank as col_5_0_, affiliatel0_.seedId as col_6_0_, affiliatel0_.plantClimbs as col_7_0_, affiliatel0_.spicy as col_8_0_, affiliatel0_.teaPlant as col_9_0_, . as col_10_0_, affiliatel0_.localizedStorefront as col_11_0_, affiliatel0_.id as col_12_0_, affiliatel0_.affiliateImageUrl as col_13_0_ from AffiliateLink affiliatel0_ inner join affiliate_keywords keywords1_ on affiliatel0_.id=keywords1_.id where affiliatel0_.id=?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题