Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Legacy Mapping Issue: @DiscriminatorColumn in an @EmbeddedId
PostPosted: Mon Oct 17, 2016 11:54 am 
Beginner
Beginner

Joined: Thu Jan 12, 2006 6:32 pm
Posts: 31
Location: Austin, Tx, USA, NA, Sol 3
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 Entity

Code:
@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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.