MappedBy refers to an unknown property of the target

I had a problem 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; 

that is

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

 @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 wrong here

+61
java orm hibernate hibernate-annotations
Oct 25 '10 at 2:14
source share
2 answers

The mappedBy attribute refers to customer , and the mCustomer property, therefore, displays an error message. Therefore, either change your mapping to:

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

Or change the entity property to customer (this is what I would do).

The mappedBy link points to "Go look at the bean property named" customer "that I have a collection to find the configuration."

+107
Oct 25 2018-10-10T00:
source share

I know that @Pascal Thivent's answer solved the problem. I would like to add a little more to his answer to others who can view this thread.

If you are like me in the early days of exploring and exploring the concept of using the @OneToMany annotation with the ' mappedBy ' property, this also means that the other side containing the @ManyToOne annotation with @JoinColumn is the 'owner of this bi-directional relationship.

In addition, mappedBy accepts the instance name (in this mCustomer example) of the Class variable as input, rather than a Class-Type (e.g. Customer) or an object name (e.g. customer).

BONUS: Also, pay attention to the orphanRemoval property of @OneToMany annotations. If it is set to true, then when a parent is deleted in a bidirectional relation, Hibernate automatically deletes its children.

+1
May 20 '19 at 23:57
source share



All Articles