Cannot save an entity with composite key of a foreign key and a local auto generated key

I cannot get @GeneratedValue to work with @IdClass if it includes a foreign key from another entity.

So what I have is an Option entity that looks like this

@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;

}

and an OptionValue Entity

@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;
}

and I try to save it

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);
    }

but I get the following exception

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]

and I cannot figure out what is wrong here

I also tried making my IdClass like this

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

but it did not work as well and showed a similar exception

1 Like

Let’s continue this discussion on stackoverflow :slight_smile:
https://stackoverflow.com/questions/65086977/cannot-get-generatedvalue-to-work-with-idclass-if-it-includes-a-foreign-key-fr