子类如何覆盖继承的关联的映射,以便将其忽略(未为子类实体映射)? [英] How can a subclass override the mapping of an inherited association so that it is ignored (not mapped for the subclass entity)?
问题描述
给出一些这样的实体:
@MappedSuperclass
public abstract class BaseEntity {
@ManyToOne(optional = true)
@JoinColumn(name = "friend_id")
private AnotherEntity friend;
}
@Entity
public class AnotherEntity {
// ...
}
@Entity
public class ConcreteEntity extends BaseEntity {
//...
}
ConcreteEntity
如何覆盖friend
的映射,以便对于ConcreteEntity
的实例完全不映射?
How can ConcreteEntity
override the mapping of friend
so that it is not mapped at all for instances of ConcreteEntity
?
推荐答案
通常,在实体中,您可以使用@AssociationOverrides
批注覆盖@MappedSuperclass
中的关系(元素),但是要强制执行忽略它.为此,存在@Transient
批注,但它不会指示JPA提供程序忽略重写的属性.因此,恕我直言,只有一种可能性:定义一个没有这种关系的SupperMappedClass,并在您的类层次结构中使用它:
In general in an entity you can override (elements of) a relationship from a @MappedSuperclass
using the @AssociationOverrides
annotation, but what you want is to force ignoring it. For that there is the @Transient
annotation, but which does not instruct the JPA provider to ignore the overridden property. So you have IMHO a single possibility: defining a SupperMappedClass without that relationship and use that in your class hierarchy:
@MappedSuperclass
public abstract class BaseEntityWithoutFriend {
}
@MappedSuperclass
public abstract class BaseEntityWithFriend {
@ManyToOne(optional = true)
@JoinColumn(name = "friend_id")
private AnotherEntity friend;
}
@Entity
public class ConcreteEntity extends BaseEntityWithoutFriend {//NOTE here that we extend the EntityWithoutFriend
//...
}
注意:根据我对Hibernate的经验,在您的实体中添加属性@Transient AnotherEntity friend
只会强制仅映射MappedSupperClass朋友属性,而仅忽略ConcreteEntity
中定义的属性.
NOTE: as of my experience with Hibernate, adding a property @Transient AnotherEntity friend
in your entity will simply force mapping only MappedSupperClass friend property, ignoring only the property defined in ConcreteEntity
.
这篇关于子类如何覆盖继承的关联的映射,以便将其忽略(未为子类实体映射)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!