-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Polymorphic Association with super class
PostPosted: Wed Jun 24, 2009 8:23 am 
Newbie

Joined: Wed Nov 05, 2008 1:17 pm
Posts: 7
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.