子进程中的JPA继承和双向OneToMany关系

时间:2015-09-16 16:33:30

标签: jpa inheritance eclipselink bidirectional

我正在尝试将一对多关联映射到使用@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 {...

继承自Weapon

的班级Item
@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...]

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

我使用了以下内容:

@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说话。