JPA OneToMany,ManyToOne双向

时间:2010-12-22 16:03:48

标签: java jpa jpa-2.0

我正试图摆脱以下错误:

  

实体中的属性[lcritical]   班级[班级   pl.pwc.docs.pl704.PL704_Error]有一个   mappedBy的值[pl704_error]   在其拥有的实体中不存在   班级[班级   pl.pwc.docs.pl704.PL704_Error_Critical。   如果拥有实体类是a   @MappedSuperclass,这是无效的,   并且您的属性应该引用   正确的子类。

PL704 @Entity class:

@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 class:

@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 class:

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

2 个答案:

答案 0 :(得分:3)

应该是

@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;   

答案 1 :(得分:1)

按属性拼写映射不正确,可能是原因:

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

@OneToMany(mappedBy="pl704_error"...

但PL704_Error_Critical中的变量仅被调用error