简体   繁体   中英

JPA OneToMany, ManyToOne bidirectional

I'm trying to get rid of the following error:

The attribute [lcritical] in entity class [class pl.pwc.docs.pl704.PL704_Error] has a mappedBy value of [pl704_error] which does not exist in its owning entity class [class pl.pwc.docs.pl704.PL704_Error_Critical]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass.

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...

Summing up, One PL704 can have many PL704_Error . One PL704_Error can have many PL704_Error_Critical .

How should I change my code to fix an error?

Used: EclipseLink 2.1.1, H2 Embedded.

It should be

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

look at the corresponding property name in PL704_Error_Critical :

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

The mapped by attribute spelling is not correct, maybe this is the cause:

In class PL704_Error the lcritical attribute is reversed mappedBy Attribute

@OneToMany(mappedBy="pl704_error"...

But the variable in PL704_Error_Critical is called only error .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM