如何使用 Hibernate 正确注释 List<Interface>?
How to properly annotate a List<Interface> with Hibernate?
我刚刚开始学习如何使用 Hibernate,但我不知道如何在引用接口时正确地注释列表。
这是我的情况,我有一个 class 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,它有两个我无法无一例外地注释的列表(私有列表 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.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.perform(EntityManagerFactoryBuilderImpl.java:850) ... 66 more
在此先感谢您的帮助!
您需要定义目标实体,以便 hibernate 可以了解绑定此关系的域。
@OneToMany(targetEntity=MarketOrderImpl.class)
@JoinTable(name="marketorderimpl")
@MapKeyColumn(name="id")
private List<MarketOrder> sellOrders;
作为 PersistenceProvider 实现 - 例如 Hibernate - 无法推断在这种情况下使用哪个具体 class 是 "correct",您必须告诉 ORM 实现在运行时使用什么类型。
在 JPA 2.1 specification 的第 11.1.40 节(PDF 第 474 页)中,我们找到了一条与您的问题相关的重要信息:
The entity class that is the target
of the association. Optional only if the collection-valued
relationship property is
defined using Java generics. Must be specified
otherwise.
提示:
Table“OneToMany
注释元素”上的 37 列出了有关如何以编程方式声明特定 OR 映射器的运行时信息的更多详细信息。
根据以上信息,正确的方法是为 targetEntity 类型声明 runtime-related 信息:
@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;
希望对您有所帮助。
我刚刚开始学习如何使用 Hibernate,但我不知道如何在引用接口时正确地注释列表。
这是我的情况,我有一个 class 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,它有两个我无法无一例外地注释的列表(私有列表 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.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.perform(EntityManagerFactoryBuilderImpl.java:850) ... 66 more
在此先感谢您的帮助!
您需要定义目标实体,以便 hibernate 可以了解绑定此关系的域。
@OneToMany(targetEntity=MarketOrderImpl.class)
@JoinTable(name="marketorderimpl")
@MapKeyColumn(name="id")
private List<MarketOrder> sellOrders;
作为 PersistenceProvider 实现 - 例如 Hibernate - 无法推断在这种情况下使用哪个具体 class 是 "correct",您必须告诉 ORM 实现在运行时使用什么类型。
在 JPA 2.1 specification 的第 11.1.40 节(PDF 第 474 页)中,我们找到了一条与您的问题相关的重要信息:
The entity class that is the target of the association. Optional only if the collection-valued relationship property is defined using Java generics. Must be specified otherwise.
提示:
Table“OneToMany
注释元素”上的 37 列出了有关如何以编程方式声明特定 OR 映射器的运行时信息的更多详细信息。
根据以上信息,正确的方法是为 targetEntity 类型声明 runtime-related 信息:
@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;
希望对您有所帮助。