0

I have a single table hierarchy shown below:

@MappedSuperclass
@Table(name = "v_contract_account", schema = "SAP")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@XmlRootElement
@XmlSeeAlso({CurrencyAccount.class, ProgramAccount.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractContractAccount implements Serializable {
    ....
}

@Entity
@Table(name = "v_contract_account", schema = "SAP")
@Immutable
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CurrencyAccount extends AbstractContractAccount  {
    ...
}

@Entity
@Table(name = "v_contract_account", schema = "SAP")
@Immutable
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("1")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProgramAccount extends AbstractContractAccount {
    ...
}

Right now it works as it is (except the discriminator stuff), but why is it that if I remove the table annotation from the subclass, Hibernate will throw an exception?

org.jboss.resteasy.spi.UnhandledException: java.lang.ClassCastException: org.jboss.resteasy.specimpl.BuiltResponse cannot be cast to [Lcom.zanox.internal.billingmasterdata.domain.entity.CurrencyAccount;

And the strange thing is, if I don't put the table and inheritance annotation in the abstract super class, everything still works fine. Does this mean MappedSuperClass doesn't care about the table and inheritance annotation? If the annotation @Inheritance(strategy = InheritanceType.SINGLE_TABLE) is not needed anywhere, then where do I specify it?

Btw, in my case here, Hibernate doesn't create the table, the table is there already and I just want to map it.

1 Answer 1

1

You probably want to remove the @MappedSuperClass annotation from your parent Entity and make it a normal Entity.

http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html

If you wanted to query across AbstractContractAccount then it has to be an Entity. You can cannot do this when it is a MappedSuperclass.

You would use @MappedSuperClass when you wanted to defined some common mapings but where there was no actual 'database inheritance'.

http://en.wikibooks.org/wiki/Java_Persistence/Inheritance

Mapped superclass inheritance allows inheritance to be used in the object model, when it does not exist in the data model. It is similar to table per class inheritance, but does not allow querying, persisting, or relationships to the superclass. Its main purpose is to allow mappings information to be inherited by its subclasses. The subclasses are responsible for defining the table, id and other information, and can modify any of the inherited mappings. A common usage of a mapped superclass is to define a common PersistentObject for your application to define common behavior and mappings such as the id and version. A mapped superclass normally should be an abstract class. A mapped superclass is not an Entity but is instead defined though the @MappedSuperclass annotation or the element.

Sign up to request clarification or add additional context in comments.

1 Comment

So MappedSuperClass's job is just really a mapping and there's no actual database inheritance. Thanks for clarifying the difference!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.