tags:

views:

116

answers:

1

I have a entity hierarchy has single table for all subclasses strategy. And I want to use each properties - mapped @ManyToOne - as primarykey. When I put @Id annotation, I'm getting this error:

java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass

So how can I provide to define all properties as identifier on discriminated hierarchy?

A: 

Your ID needs to be unique for the highest object in the hierarchy that is an entity. You can't define ID on subclasses and have an superclass, that is an entity, that has no ID.

Think of it this way, entityManager.find(SuperClass.class, 2); is a perfectly legal call. If ID were defined on subclasses as an FK column, more than one of them could have ID 2! What would be returned?

Using the FK side of a ManyToOne relationship as a primary key is nonsensical in the first place. It would by definition be a OneToOne then.

Affe