I am working with a legacy schema that I've mapped out as follows. The problem is that the @DiscriminatorColumn annotation is a column definition on its own instead of a pointer back to an already defined column (such as @JoinColumn (which has the i/n/u=false parameters and can be set to ignore the already existing column)) and I am ending up with the error:
Code:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: gov.dps.dld.dls.db.domain.ApplicantHomePhone column: CTIN_CONTACT_TYP (should be mapped with insert="false" update="false"
Ancestor EntityCode:
@DiscriminatorColumn(name = "CTIN_CONTACT_TYP", discriminatorType = DiscriminatorType.STRING, length = 2)
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@MappedSuperclass
@Table(catalog = "", name = "CONTACT_INFO")
public abstract class ContactInfo implements Serializable {
private ContactInfoId id;
where the table's trinary key is:
Code:
@Embeddable
public class ContactInfoId implements java.io.Serializable {
private Long personId; // decimal(17), not null, [b]related key from parent table[/b]
private ContactInfoTypeEnum contactInfoTypeEnum; // char(2), not null
private Timestamp recordDate; // timestamp, not null
...
@Column(name = "CTIN_CONTACT_TYP", insertable = true, nullable = false, updatable = false, length = FIELD_LEN_MAX_CONTACT_TYPE)
@Enumerated(EnumType.STRING)
@NotNull(message = "contactInfo.id.contactInfoTypeEnum.required")
public ContactInfoTypeEnum getContactInfoTypeEnum() {
return contactInfoTypeEnum;
}
and the three descendant classes are:
Code:
@DiscriminatorValue("EM")
@Entity
public class ApplicantEmail extends ContactInfo {
public ApplicantEmail(){
super();
super.getId().setContactInfoTypeEnum(ContactInfoTypeEnum.APPLICANT_EMAIL);
}
Code:
@DiscriminatorValue("HP")
@Entity
public class ApplicantHomePhone extends ContactInfo {
public ApplicantHomePhone() {
super();
super.getId().setContactInfoTypeEnum(ContactInfoTypeEnum.HOME_PHONE_INFO);
}
Code:
@DiscriminatorValue("OP")
@Entity
public class ApplicantOtherPhone extends ContactInfo {
public ApplicantOtherPhone() {
super();
super.getId().setContactInfoTypeEnum(ContactInfoTypeEnum.OTHER_PHONE_INFO);
}
It took me a bit to understand why this was happening but I think the biggest obstacle is the fact that the DB's discriminator is part of the PK and I'm guessing Hibernate doesn't care for that? (Rather stupid restriction since this is a very common pattern.)
At this point, the only thing I can think of to get this to work is to add an identity column which could be used as a simple @Id, moving the three fields from the compound key back to the main entity, allowing the personId field to still be used as the FK back to the Person entity, and the contactInfoTypeEnum field could still be used, but alas no.
Because of @DiscriminatorColumn wanting to ADD a column instead of referencing one, I still get the error.
Is it not possible for me to reference the discriminator column as an @Enumerated value?