简体   繁体   English

如何使用JPA Annotations指定字段的类?

[英]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: 我有一个场景,我希望我的EntityA的一个字段被这样的抽象非持久化超类引用:

@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 当然,hibernate抱怨它

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. 但是我可以指定将该字段向下转换为实际的已知EntityB类型。 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? 我想我可以用xml映射做到这一点,对吗? Class can be specified in there. 可以在那里指定类。

Thanks! 谢谢!

@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? 如果您知道它必须始终是EntityB,为什么不使用EntityB作为字段的类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM