Java/Hibernate 在实体上使用接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2799365/
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-29 22:54:16  来源:igfitidea点击:

Java/Hibernate using interfaces over the entities

javahibernateinterface

提问by wen

I am using annoted Hibernate, and I'm wondering whether the following is possible.

我正在使用带注释的 Hibernate,我想知道以下是否可行。

I have to set up a series of interfaces representing the objects that can be persisted, and an interface for the main database class containing several operations for persisting these objects (... an API for the database).

我必须设置一系列表示可以持久化的对象的接口,以及一个包含多个用于持久化这些对象的操作的主数据库类的接口(...数据库的 API)。

Below that, I have to implement these interfaces, and persist them with Hibernate.

在此之下,我必须实现这些接口,并使用 Hibernate 将它们持久化。

So I'll have, for example:

所以我会有,例如:

public interface Data {
  public String getSomeString();
  public void setSomeString(String someString);
}

@Entity
public class HbnData implements Data, Serializable {
  @Column(name = "some_string")
  private String someString;

  public String getSomeString() {
    return this.someString;
  }
  public void setSomeString(String someString) {
    this.someString = someString;
  }
}

Now, this works fine, sort of. The trouble comes when I want nested entities. The interface of what I'd want is easy enough:

现在,这工作正常,有点。当我想要嵌套实体时,麻烦就来了。我想要的界面很简单:

public interface HasData {
  public Data getSomeData();
  public void setSomeData(Data someData);
}

But when I implement the class, I can follow the interface, as below, and get an error from Hibernate saying it doesn't know the class "Data".

但是当我实现这个类时,我可以按照下面的接口,并从 Hibernate 得到一个错误,说它不知道类“Data”。

@Entity
public class HbnHasData implements HasData, Serializable {
  @OneToOne(cascade = CascadeType.ALL)
  private Data someData;

  public Data getSomeData() {
    return this.someData;
  }

  public void setSomeData(Data someData) {
    this.someData = someData;
  }
}

The simple change would be to change the type from "Data" to "HbnData", but that would obviously break the interface implementation, and thus make the abstraction impossible.

简单的更改是将类型从“Data”更改为“HbnData”,但这显然会破坏接口实现,从而使抽象变得不可能。

Can anyone explain to me how to implement this in a way that it will work with Hibernate?

谁能向我解释如何以一种适用于 Hibernate 的方式实现它?

回答by Sam Donnelly

Maybe OneToOne.targetEntity?:

也许 OneToOne.targetEntity?:

@OneToOne(targetEntity = HbnData.class, cascade = CascadeType.ALL)
private Data someData;

回答by duffymo

The interface that I usually use is Data Access Object, or DAO. Using Java generics, I can write it just once; Hibernate makes it possible to write the implementation just once, too:

我通常使用的接口是数据访问对象,或 DAO。使用Java泛型,我可以只写一次;Hibernate 也可以只编写一次实现:

package persistence;

import java.io.Serializable;
import java.util.List;

public interface GenericDao<T, K extends Serializable>
{
    T find(K id);
    List<T> find();
    List<T> find(T example);
    List<T> find(String queryName, String [] paramNames, Object [] bindValues);

    K save(T instance);
    void update(T instance);
    void delete(T instance);
}