javahibernateormhibernate-annotations

mappedBy reference an unknown target entity property


I am having an issue in setting up a one to many relationship in my annotated object.

I have the following:

@MappedSuperclass
public abstract class MappedModel
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id",nullable=false,unique=true)
    private Long mId;

then this

@Entity
@Table(name="customer")
public class Customer extends MappedModel implements Serializable
{

    /**
   * 
   */
  private static final long serialVersionUID = -2543425088717298236L;


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

and this

@Entity
@Table(name="store")
public class Store extends MappedModel implements Serializable
{

    /**
   * 
   */
  private static final long serialVersionUID = -9017650847571487336L;

  /** many stores have a single customer **/
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn (name="customer_id",referencedColumnName="id",nullable=false,unique=true)
  private Customer mCustomer;

what am i doing incorrect here


Solution

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