【问题标题】:JPA OneToMany, ManyToOne bidirectionalJPA OneToMany、ManyToOne 双向
【发布时间】:2010-12-22 16:03:48
【问题描述】:

我正在尝试摆脱以下错误:

实体中的属性[lcritical] 类[类 pl.pwc.docs.pl704.PL704_Error] 有一个 mappedBy [pl704_error] 的值 不存在于其拥有的实体中 类[类 pl.pwc.docs.pl704.PL704_Error_Critical]。 如果拥有实体类是 @MappedSuperclass,这是无效的, 你的属性应该参考 正确的子类。

PL704 @Entity 类:

@Entity  
public class PL704 implements Serializable {  
    private static final long serialVersionUID = 1L;  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    private Long id;  
    private int Status;  
    private String Comments;  
    @OneToMany(mappedBy="pl704", cascade=CascadeType.ALL, targetEntity=PL704_Error.class, fetch=FetchType.EAGER)  
    private Collection lerror = new ArrayList<PL704_Error>();

    //getters, setters...  

PL704_Error @Entity 类:

@Entity  
public class PL704_Error implements Serializable {  
    private static final long serialVersionUID = 1L;  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    private Long id;  
    private String ErrorType;  
    private String ErrorReason;  
    private String ErrorLocation;  
    private String OriginalAttributeValue;  
    @ManyToOne  
    @JoinColumn(name = "PL704_ID", referencedColumnName = "ID")  
    private PL704 pl704;  

    @OneToMany(mappedBy="pl704_error", cascade=CascadeType.ALL,     targetEntity=PL704_Error_Critical.class, fetch=FetchType.EAGER)  
    private Collection lcritical = new ArrayList<PL704_Error_Critical>();

    //getters, setters...

PL704_Error_Critical @Entity 类:

@Entity  
public class PL704_Error_Critical implements Serializable {  
    private static final long serialVersionUID = 1L;  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    private Long id;  
    @ManyToOne(cascade=CascadeType.ALL)  
    @JoinColumn(name = "PL704_ERROR_ID", referencedColumnName = "ID")  
    private PL704_Error error;  

    //getters, setters...

总结一下,一个PL704 可以有多个PL704_Error。一个PL704_Error 可以有多个PL704_Error_Critical

我应该如何更改我的代码来修复错误?

使用:EclipseLink 2.1.1,H2 Embedded。

【问题讨论】:

    标签: java jpa jpa-2.0


    【解决方案1】:

    应该是

    @OneToMany(mappedBy="error", cascade=CascadeType.ALL,
        targetEntity=PL704_Error_Critical.class, fetch=FetchType.EAGER)
    private Collection lcritical = new ArrayList<PL704_Error_Critical>(); 
    

    查看PL704_Error_Critical中对应的属性名:

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "PL704_ERROR_ID", referencedColumnName = "ID")       
    private PL704_Error error;   
    

    【讨论】:

      【解决方案2】:

      mapped by attribute 拼写不正确,可能是这个原因:

      在类 PL704_Error 中,lcritical 属性被反转 mappedBy 属性

      @OneToMany(mappedBy="pl704_error"...
      

      但是 PL704_Error_Critical 中的变量只调用了error

      【讨论】:

      • 好的,但现在我有一些奇怪的事情:“异常描述:[class pl.pwc.docs.pl704.PL704_Error] 使用非实体 [class pl.pwc.docs.pl704.PL704_Error_Critical]作为关系属性 [字段 lcritical] 中的目标实体。”
      • 什么都没有改变,5分钟后它神奇地开始工作了。
      • 上面还是有异常。
      • @monczek:你解决过上述异常吗?我也有同样的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多