mappedBy reference an unknown target entity property mappedBy reference an unknown target entity property java java

mappedBy reference an unknown target entity property


The mappedBy attribute is referencing customer while the property is mCustomer, hence the error message. So either change your mapping into:

/** The collection of stores. */@OneToMany(mappedBy = "mCustomer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)private Collection<Store> stores;

Or change the entity property into customer (which is what I would do).

The mappedBy reference indicates "Go look over on the bean property named 'customer' on the thing I have a collection of to find the configuration."


I know the answer by @Pascal Thivent has solved the issue. I would like to add a bit more to his answer to others who might be surfing this thread.

If you are like me in the initial days of learning and wrapping your head around the concept of using the @OneToMany annotation with the 'mappedBy' property, it also means that the other side holding the @ManyToOne annotation with the @JoinColumn is the 'owner' of this bi-directional relationship.

Also, mappedBy takes in the instance name (mCustomer in this example) of the Class variable as an input and not the Class-Type (ex:Customer) or the entity name(Ex:customer).

BONUS : Also, look into the orphanRemoval property of @OneToMany annotation. If it is set to true, then if a parent is deleted in a bi-directional relationship, Hibernate automatically deletes it's children.


public class User implements Serializable {    private static final long serialVersionUID = 1L;    @Id    @Column(name = "USER_ID")    Long userId;    @OneToMany(fetch = FetchType.LAZY, mappedBy = "sender", cascade = CascadeType.ALL)    List<Notification> sender;    @OneToMany(fetch = FetchType.LAZY, mappedBy = "receiver", cascade = CascadeType.ALL)    List<Notification> receiver;}public class Notification implements Serializable {    private static final long serialVersionUID = 1L;    @Id    @Column(name = "NOTIFICATION_ID")    Long notificationId;    @Column(name = "TEXT")    String text;    @Column(name = "ALERT_STATUS")    @Enumerated(EnumType.STRING)    AlertStatus alertStatus = AlertStatus.NEW;    @ManyToOne(fetch = FetchType.LAZY)    @JoinColumn(name = "SENDER_ID")    @JsonIgnore    User sender;    @ManyToOne(fetch = FetchType.LAZY)    @JoinColumn(name = "RECEIVER_ID")    @JsonIgnore    User receiver;}

What I understood from the answer. mappedy="sender" value should be the same in the notification model. I will give you an example..

User model:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "**sender**", cascade = CascadeType.ALL)    List<Notification> sender;    @OneToMany(fetch = FetchType.LAZY, mappedBy = "**receiver**", cascade = CascadeType.ALL)    List<Notification> receiver;

Notification model:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "sender", cascade = CascadeType.ALL)    List<Notification> **sender**;    @OneToMany(fetch = FetchType.LAZY, mappedBy = "receiver", cascade = CascadeType.ALL)    List<Notification> **receiver**;

I gave bold font to user model and notification field. User model mappedBy="sender " should be equal to notification List sender; and mappedBy="receiver" should be equal to notification List receiver; If not, you will get error.