I'm switching from hbm xml to annotations (in order to use the envers framework) and have run across something I don't know how to express using annotations.
Code:
public abstract class AbstractA implements AInterface {}
public class P implements PInterface {
private AInterface myA;
}
public class ASubclass1 extends AbstractA {}
public class ASubclass2 extends AbstractA {}
Currently (and successfully), I'm using the following hbm to map this (IDs omitted for brevity):
Code:
<class name="P" table="P_TABLE">
<many-to-one
name="A"
class="AbstractA"
cascade="save-update,delete"
column="ID"
not-null="true"
unique="true"
/>
...
<class name="AbstractA" table="A_TABLE">
<discriminator column="A_TYPE" type="string" />
</class>
I tried to map it with annotations as follows:
Code:
@Entity
@Table( name="A_TABLE" )
@Inheritance( strategy=InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn( name="A_TYPE", discriminatorType=DiscriminatorType.STRING )
@DiscriminatorValue( "INA" )
public abstract class AbstractA implements AInterface {}
@Entity
@Table( name="P_TABLE" )
public class P implements PInterface {
@Cascade( value = { CascadeType.SAVE_UPDATE, CascadeType.DELETE } )
@JoinColumn( name="A_ID", nullable=false, unique=true )
@ManyToOne( targetEntity = AbstractA.class )
private AInterface myA;
}
@Entity
@DiscriminatorValue(value="INA")
public class ASubclass1 extends AbstractA {}
@Entity
@DiscriminatorValue(value="ENA")
public class ASubclass2 extends AbstractA {}
I was forced to decorate AbstractA as an @Entity or hibernate complained about the P relationship mapping (reference to an unknown entity). Intuitively, I'd like to map AbstractA as a @MappedSuperclass, but am not sure how to do this. Can someone help?