java JPA 不能很好地支持接口.. 影响?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4204868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:18:31  来源:igfitidea点击:

JPA doesn't support interfaces well..implications?

javajpa

提问by Aravind Yarram

I was going through some posts in Stackoverflow on JPA and I read multiple places that JPA does not support interfaces. Can someone please share what it means in a real-world project. Does this mean we cannot annotate the interface?

我正在阅读有关 JPA 的 Stackoverflow 中的一些帖子,并且我阅读了多个 JPA 不支持接口的地方。有人可以分享它在实际项目中的含义吗?这是否意味着我们不能对接口进行注释?

回答by James

What this means is that you cannot map (annotate) or query on an inferface. You can only query @Entity classes, and these can only be placed on real classes, not interfaces. Normally this is not an issue, an interface has no state, so is not something that is really relevant to persistence most of the time. You can still use interfaces in your model, you just can't map them directly.

这意味着您不能在接口上映射(注释)或查询。你只能查询@Entity 类,而且这些只能放在真正的类上,不能放在接口上。通常这不是问题,接口没有状态,因此大多数时候与持久性无关。您仍然可以在模型中使用接口,只是不能直接映射它们。

If you have a relationship that uses an interface type, you just need to set the targetEntity to the implementation class. If you have multiple implementers and can't make them share inheritance then you need to get more creative. Some JPA providers such as EclipseLink have support for interfaces.

如果你有一个使用接口类型的关系,你只需要将 targetEntity 设置为实现类。如果您有多个实施者并且不能让他们共享继承,那么您需要获得更多创意。某些 JPA 提供程序(例如 EclipseLink)支持接口。

See, http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces

见, http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces

回答by Sean Patrick Floyd

In JPA, use @MappedSuperclassfor inheritance, annotate abstract classes, not interfaces.

在JPA,使用@MappedSuperclass了继承,注释抽象类,而不是接口。

I tend to use common base classes for all my entities in a given project:

我倾向于为给定项目中的所有实体使用通用基类:

@MappedSuperclass
public abstract class BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
}

Now let's say that some of my entities have additional behavior in common: automatically updated timestamps for createdDate and updatedDate

现在让我们说我的一些实体有额外的共同行为:为 createdDate 和 updatedDate 自动更新时间戳

@MappedSuperclass
public abstract class ExtendedEntity extends BaseEntity{

    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedDate;

    @PrePersist
    protected void creationTimeStamp(){
        createdDate = new Date();
        this.updateTimeStamp();
    }

    @PreUpdate
    protected void updateTimeStamp(){
        updatedDate = new Date();
    }

}

Now, some of my entities extend BaseEntitydirectly, and others extend ExtendedEntity(if they need timestamps).

现在,我的一些实体BaseEntity直接扩展,其他实体扩展ExtendedEntity(如果他们需要时间戳)。

You can also configure the way the inheritance is modeled:

您还可以配置继承建模的方式

  • A single table per class hierarchy
  • A table per concrete entity class (default)
  • A “join” strategy, whereby fields or properties that are specific to a subclass are mapped to a different table than the fields or properties that are common to the parent class
  • 每个类层次结构一个表
  • 每个具体实体类的表(默认)
  • “连接”策略,其中特定于子类的字段或属性映射到与父类共有的字段或属性不同的表

BTW: I think the choice not to support interfaces is a good one. Interfaces are designed to model behavior, not state. JPA is about managing state, not behavior, so the two concepts don't fit together.

BTW:我认为不支持接口是一个很好的选择。接口旨在模拟行为,而不是状态。JPA 是关于管理状态,而不是行为,因此这两个概念不适合在一起。

And in cases where you think you need multiple inheritance, perhaps @Embeddableis the solution

在您认为需要多重继承的情况下,也许@Embeddable是解决方案

回答by duffymo

You need a proxy generator for an interface or an implementation that you provide.

您需要一个用于您提供的接口或实现的代理生成器。

Can you post the links to the SO answers you're thinking of so we can get the complete context? That might help answer the question.

您能否发布指向您正在考虑的 SO 答案的链接,以便我们获得完整的上下文?这可能有助于回答这个问题。