【问题标题】:How to properly annotate a List<Interface> with Hibernate?如何使用 Hibernate 正确注释 List<Interface>?
【发布时间】:2018-03-18 02:16:33
【问题描述】:

我刚刚开始学习如何使用 Hibernate,但我无法弄清楚在引用接口时如何正确注释列表。

这是我的情况,我有一个像这样实现 MarketOrder 的类 MarketOrderImpl...

@Entity
public final class MarketOrderImpl implements MarketOrder {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
private OrderType type;
private BigDecimal price;
private BigDecimal quantity;
private BigDecimal total;

public MarketOrderImpl(OrderType type, BigDecimal price, BigDecimal 
quantity, BigDecimal total) {
    this.type = type;
    this.price = price;
    this.quantity = quantity;
    this.total = total;
}

我有一个 MarketOrderBookImpl,它有两个列表,我无法毫无例外地注释(私有列表 sellOrders 和私有列表 buyOrders)

@Entity
public final class MarketOrderBookImpl implements MarketOrderBook {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String marketId;

@OneToMany @JoinTable(name="marketorderimpl")
@MapKeyColumn(name="id")
private List<MarketOrder> sellOrders;

@OneToMany @JoinTable(name="marketorderimpl")
@MapKeyColumn(name="id")
private List<MarketOrder> buyOrders;


public MarketOrderBookImpl(String marketId, List<MarketOrder> 
sellOrders, List<MarketOrder> buyOrders) {
    this.marketId = marketId;
    this.sellOrders = sellOrders;
    this.buyOrders = buyOrders;
}

这是我的例外...

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:br.com.codefleck.tradebot.exchanges.trading.api.impl.MarketOrderBookImpl.sellOrders[br.com.codefleck.tradebot.tradingapi.MarketOrder]at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1136) at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:792)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:727)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ... 66 more

提前感谢您的帮助!

【问题讨论】:

  • @Mohammad 我不同意,这是一个不同的问题,因为它是关于不同实体之间的映射和多样性。

标签: java hibernate jpa annotations jpa-2.1


【解决方案1】:

作为 PersistenceProvider 实现 - 例如 Hibernate - 无法推断在这种情况下哪个具体类是“正确”的类,您必须告诉 ORM 实现使用什么类型在运行时。

JPA 2.1 specification 的第 11.1.40 节(PDF 第 474 页)中,我们发现了一条与您的问题相关的重要信息:

作为目标的实体 协会的。仅当 collection-valued 时才可选 关系属性是 使用 Java 泛型定义。必须指定 否则。

提示

OneToMany Annotation Elements”上的表 37 列出了有关如何以编程方式为特定 OR 映射器声明运行时信息的更多详细信息。

鉴于以上信息,正确的做法是为 targetEntity 类型声明运行时相关信息:

@OneToMany(targetEntity= MarketOrderImpl.class) 
@JoinTable(name="marketorderimpl")
@MapKeyColumn(name="id")
private List<MarketOrder> sellOrders;

@OneToMany(targetEntity= MarketOrderImpl.class) 
@JoinTable(name="marketorderimpl")
@MapKeyColumn(name="id")
private List<MarketOrder> buyOrders;

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您需要定义目标实体,以便hibernate 可以了解绑定此关系的域。

    @OneToMany(targetEntity=MarketOrderImpl.class) @JoinTable(name="marketorderimpl") @MapKeyColumn(name="id") private List<MarketOrder> sellOrders;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2014-08-25
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多