monczek
monczek

Reputation: 1142

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.

Upvotes: 0

Views: 7858

Answers (2)

Ralph
Ralph

Reputation: 120871

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.

Upvotes: 1

axtavt
axtavt

Reputation: 242786

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;   

Upvotes: 3

Related Questions