JPA Inheritance - Discriminator Column as part of Primary Key - DiscriminatorOptions not working?

Good morning, I am having an issue with using an @DiscriminatorColumn as part of a composite primary key.
I have used the @DiscriminatorOptions(insert=false) to try and prevent the duplicate insertions when the query is parsed but I still seem to get the duplicated inserted column.


@Embeddable
public class MyCompositeID implements Serializable {

    @Column(name = "parent_id")
    private Integer parent;

    @Column(name = "country_code")
    private String countryCode;

    @Enumerated(EnumType.STRING)
    @Column(name = "payment_method",updatable = false,insertable = false)
    private PaymentMethod paymentMethod;
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "payment_method")
@DiscriminatorOptions(insert = false)
@Table(name = "parent_payment_params")
public abstract class PaymentParam {

    @EmbeddedId
    private MyCompositeId id = new MyCompositeId();

    public void setCountryCode(String cc){
        this.id.setCountryCode(cc);
    }

    @JoinColumn(name = "parent_id")
    @MapsId("parent")
    @ManyToOne(fetch = FetchType.LAZY)
    private Parent parent;

}

The basic idea is a parent has PaymentParams that are country and method specific.
So a Parent has many of these PaymentParams that have a composite ID of the countrycode and the payment method.
The inheritance is on the payment method, so there are say:

CreditCardPaymentParams and WebFormPaymentParams etc. extending this base PaymentParams

org.hibernate.exception.SQLGrammarException: could not prepare statement [Duplicate column name "PAYMENT_METHOD"; SQL statement:
insert into parent_payment_params (payment_method,country_code,parent_id,payment_method) values ('CC',?,?,?) [42121-224]] [insert into parent_payment_params (payment_method,country_code,parent_id,payment_method) values ('CC',?,?,?)]

Thank you for any help! I am not sure if I am missing something.

Your mapping looks correct to me, but why are you also setting the paymentMethod column in the embedded id as insertable = false? In any case, this looks like a bug, so please create a simplified reproducer using our test case templates and open a new issue in our tracker.

I have created the issue - Sorry it is my first go so it could be terrible.

https://hibernate.atlassian.net/browse/HHH-18910

As far as adding the “insertable=false” I usually get a warning when you have to mark a column as insertable false updateable false when you have an entity with a mapsId in the composite key (which I do but may have omitted for this test case):

1 Like