kboom
kboom

Reputation: 2339

How to specify a class of a field using JPA Annotations?

I have a scenario where I'd like one of the fields of my EntityA be referred to by an abstract-non-persistable superclass like this:

@Entity
public class EntityA {

    @ManyToOne
    @JoinColumn(name = "id")
    private AbstractB upcastEntityB; // it's really a superclass for the entity which I'd like to persist

}

Of course hibernate complains about it with

references an unknown entity

since this class is not known to it. But I could specify that this field is to be downcast to the known EntityB type which it really is. Is there a way to do so? I really need to do this, I know this is awkward.

I suppose I could do this with xml mapping, am I right? Class can be specified in there.

Thanks!

Upvotes: 0

Views: 32

Answers (1)

JB Nizet
JB Nizet

Reputation: 691775

@ManyToOne(targetEntity = EntityB.class)

I don't understand why this would be useful though. If you know it must be and always be an EntityB, why not use EntityB as the type of the field?

Upvotes: 2

Related Questions