如何使用Hibernate正确注释List <interface>?

时间:2018-03-18 02:16:34

标签: java hibernate jpa annotations jpa-2.1

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

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

@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,它有两个列表,我无法在没有例外的情况下进行旋转(私有List sellOrders和私有List 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

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

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

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

  

作为目标的实体   该协会。仅在集合值时可选   关系属性是   使用Java泛型定义。必须指定   否则。

提示

OneToMany注释元素”表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;

希望它有所帮助。

答案 1 :(得分:2)

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

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