[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Inheritance mapping question.

allanjun do-not-reply at jboss.com
Wed Apr 15 22:09:02 EDT 2009


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.




View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4225773#4225773

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4225773



More information about the jboss-user mailing list