尝试使用Spring数据检索ElementCollection时出现SQL错误

时间:2018-08-02 10:04:11

标签: java mysql spring hibernate spring-data

我有一个名为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的DTO对象的构造函数来获取必填字段。该对象扩展了另一个名为AffiliateLinkCreateDTO的类,因为它们都共享相似的字段SQL语法错误是由s.keywords引起的,错误消息为:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您有一个   您的SQL语法错误;检查与您的手册相对应的手册   MySQL服务器版本,用于在'as col_10_0_附近使用正确的语法   来自AffiliateLink会员l0_内部加入affiliate_keywords关键字'   在第1行

由于某种原因,它试图在inner join被命名为affiliate_keywords keywo的同时在Collection 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;
    }

由UpdateDTO类扩展的AffiliateLinkCreateDTO

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=?

0 个答案:

没有答案