views:

37

answers:

0

I currently have a mapping problem and no idea how to solve it. Here is what we currently have.

AbstractEntity is a @MappedSuperclass

ExtendedEntity is an abstract @Entity extending AbstractEntity with InheritanceType.TABLE_PER_CLASS

Proposal is a subclass of ExtendedEntity that has been stored in a single table so far.

Now I want to split Proposal into different Subclasses using InheritanceType.SINGLE_TABLE from now on.

The JPA docs say, that a once defined InheritanceType is used for the whole subtree and can't be changed. Hibernate docs say, it is supported in that particular framework. We use Hibernate v3.3.1.GA. But the mapping does not switch from TABLE_PER_CLASS to SINGLE_TABLE for my Proposal.

So I've tried to define ExtendedEntity as @MappedSuperclass - but that doesn't work because we have @OneToMany and other associations to that type. I'm stuck with @Entity here. But if I try to remove the InheritanceType from this class Hibernate starts to query a table called ExtendedEntity although this class is abstract and actually stored in separate tables for all subclasses.

Is there any way to switch from TABLE_PER_CLASS to SINGLE_TABLE that actually works?

Here some code snippets for more details:

@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long                id;
    ...
}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ExtendedEntity extends AbstractEntity {
    ...
}

@Entity
@Table(name = Proposal.TABLE_NAME)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = Proposal.DISCRIMINATOR_COLUMN, discriminatorType = DiscriminatorType.STRING)
public abstract class Proposal extends ExtendedEntity {
    ...
}

@Entity
@DiscriminatorValue(value = ProposalOne.DISCRIMINATOR_VALUE)
public class ProposalOne extends Proposal {
    ...
}

@Entity
@DiscriminatorValue(value = ProposalTwo.DISCRIMINATOR_VALUE)
public class ProposalTwo extends Proposal {
    ...
}

related questions