【问题标题】:JPA inheritance and bidirectional OneToMany relation in child孩子中的JPA继承和双向OneToMany关系
【发布时间】:2015-09-16 16:33:29
【问题描述】:

我正在尝试将关联一对多映射到与 @Inheritance(strategy=InheritanceType.JOINED) 映射的实体,但在我的应用程序加载时出现验证异常:

内部异常:异常 [EclipseLink-7250](Eclipse 持久性 服务 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException 异常

描述:[class model.item.weapon.WeaponStat] 使用非实体 [class item.weapon.Weapon] 作为关系中的目标实体 属性[野战武器]。

这里是基类:

@Entity
@Table(name = "ITEMS")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "SYSTYPE", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue(value = "0")
public class Item implements Serializable {...

继承自Item的类Weapon

@Entity
@Table(name = "WEAPONS")
@PrimaryKeyJoinColumn(name = "ITEM_ID")
@DiscriminatorValue(value = "3")
public class Weapon extends Item {...

Weapon 包含OneToMany 属性:

@OneToMany(mappedBy = "weapon")
private List<WeaponStat> stats;

还有WeaponStat

@Entity
@Table(name = "WEAPON_STATS")
public class WeaponStat implements Serializable {...
[...code...]
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "WEAPON_ID")
private Weapon weapon;
[...code...]

有人有想法吗?

【问题讨论】:

  • Weapon 是否与persistence.xml 文件中的所有其他实体一起正确列出?是否使用正确的 javax.persistence.Entity 注释进行了注释?
  • 我实际上在我的 persistence.xml 中使用了&lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;。如果我用&lt;class&gt;...&lt;/class&gt; 手动指定所有实体,问题是一样的。是的,进口是正确的。
  • 你不能把它设为私人武器武器; => 私人物品武器;我知道这并不总是实用的,但武器上的成员与物品上的成员不同违反了 LSP en.wikipedia.org/wiki/Liskov_substitution_principle
  • 谢谢,确实是这个问题!但是 :( Exception Description: Missing class for indicator field value [3] of type [class java.lang.Integer]. Descriptor: RelationalDescriptor(model.item.Item --&gt; [DatabaseTable(ITEMS)]) 当我尝试访问 Weapon 时。如果我访问一个简单的 Item 一切都很好......
  • 为什么要结合继承类型JOINED和判别器?

标签: jpa inheritance eclipselink bidirectional


【解决方案1】:

我使用了以下内容:

@Entity
@Configuration
class Parent()
{
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(nullable = true)
    private Set<Child> children;
    ...
}

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Child
{
 ...
}

@Entity
public class NaughtyChild extends Child
{
...
}

这是使用 Hibernate,但希望对您有所帮助。请注意,这是一个奇怪的配置,Hibernate 不建议这样做,至少因为生成的非优化查询是特定于 Hibernate 的。我无法与 EclipseLink 通话。

【讨论】:

  • 同样的问题 :( @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy = "weapon") @JoinColumn(nullable = true) private List&lt;WeaponStat&gt; stats;
猜你喜欢
  • 2010-10-27
  • 2017-06-23
  • 2023-03-20
  • 2013-06-17
  • 2018-11-29
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多