如果@idclass包含来自另一个实体的外键,则无法获取@generatedvalue以使用它

tzxcd3kk  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(287)

如果@idclass包含来自另一个实体的外键,则无法使用@generatedvalue。
所以我有一个选项实体,看起来像这样

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "options")
public class Option extends UserDateAudit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "option_id")
    private Long optionId;

    @NotBlank
    @Column(nullable = false)
    private String name;

    //one to many with optionValues entity
    @OneToMany(mappedBy = "option", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private Set<OptionValue> optionValues;

    @OneToMany(mappedBy = "option", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<ProductOption> optionProducts;

}

以及optionvalue实体

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "option_values")
@IdClass(OptionValueId.class)
public class OptionValue extends UserDateAudit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "option_value_id")
    private Long optionValueId;

    @NotBlank
    @Column(nullable = false)
    private String valueName;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "option_id", referencedColumnName = "option_id")
    private Option option;

    @OneToMany(mappedBy = "optionValue", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<VariantValue> variantValues;

}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OptionValueId implements Serializable {
    private Long optionValueId;
    private Option option;
}

我试着拯救它

public ResponseEntity<OptionValue> create(Long optionId, OptionValueCreateDto optionValueCreateDto) {
        Option option = optionRepository.findById(optionId).orElseThrow(
                () -> new EntityNotFoundException("errors.option.notFound")
        );
        OptionValue optionValue = ObjectMapperUtils.map(optionValueCreateDto, OptionValue.class);
        optionValue.setOption(option);
        optionValue = optionValueRepository.save(optionValue);
        return new ResponseEntity<>(optionValue, HttpStatus.CREATED);
    }

但我有以下例外

Resolved [org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'com.ecommerce.product.model.Option' for property 'option'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'com.ecommerce.product.model.Option' for property 'option': no matching editors or conversion strategy found]

我不知道这里出了什么问题
我也试着让我的课变成这样

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OptionValueId implements Serializable {
    @Column(name = "option_value_id")
    private Long optionValueId;
    @Column(name = "option_id")
    private Long option;
}

但效果并不理想,出现了类似的例外

yqkkidmi

yqkkidmi1#

异常与spring数据或webmvc无法转换值有关。将生成的标识符与其他标识符混合是不可能的。为什么你需要这两个价值观呢?在这里使用复合id是没有意义的。就用这个:

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "option_values")
@IdClass(OptionValueId.class)
public class OptionValue extends UserDateAudit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "option_value_id")
    private Long optionValueId;

    @NotBlank
    @Column(nullable = false)
    private String valueName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "option_id", referencedColumnName = "option_id")
    private Option option;

    @OneToMany(mappedBy = "optionValue", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<VariantValue> variantValues;

}

相关问题