0 Replies Latest reply on Apr 15, 2009 10:08 PM by Jun Li

    Inheritance mapping question.

    Jun Li Newbie

      Hi there,

      The DB I'm working on has a generic table called REF_CODES to store all types of codes and their descriptions. eg, address type, contact type ect.

      schema of REF_CODES:
      domain, code, description (domain and code are pk columns)

      example data
      add_type, P, postal
      add_type, R, residential
      contact_type,G, general
      contact_type,O, other

      what I wanted to do is do inheritance mapping as follow:

      @Entity
      @Table(name = "ref_codes")
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name = "domain")
      public abstract class RefCodes {
      @EmbeddedId
      private RefCodesId id;

      @Embeddable
      private class RefCodesId implements Serializable {

      @Column(name = "domain")
      private String domain;
      @Column(name = "code")
      private String code;

      }
      }

      @Entity
      @DiscriminatorValue(value = "add_type")
      public class AddressType extends RefCodes implements Serializable {

      @Column(name = "code")
      private String code;

      @Column(name = "description")
      private String description;
      }

      And then the AddressType can be used by Address class in ManyToOne mapping as follow;
      @Entity
      @Table(name = "addresses")
      public class Address implements Serializable {

      private String name;

      @ManyToOne
      @Joincolumn(name = "add_type_code")
      private AddressType type;

      }

      The problem I'm having is that in Address class the ManyToOne mapping to AddressType needs to join 2 columns as AddressType(RefCode) has a composite PK, but the Address table only stores address code, how do I let the join query know the value of the domain as 'add_type' then.
      Or, is there a way to override the id of AddressType class as 'code'?

      Is this mapping doable? Thanks.