Java mappingBy 引用了一个未知的目标实体属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4011472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:21:28  来源:igfitidea点击:

mappedBy reference an unknown target entity property

javahibernateormhibernate-annotations

提问by boyd4715

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

我在这里做错了什么

采纳答案by Pascal Thivent

The mappedByattribute is referencing customerwhile the property is mCustomer, hence the error message. So either change your mapping into:

mappedBy属性为customer时,该属性正在引用mCustomer,因此会出现错误消息。因此,要么将您的映射更改为:

/** 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).

或者将实体属性更改为customer(这是我要做的)。

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

mappingBy 引用表示“在我收集的事物上查看名为‘customer’的 bean 属性以查找配置。”

回答by Dhyanesh

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

我知道@Pascal Thivent的回答已经解决了这个问题。我想在他对可能浏览此线程的其他人的回答中添加更多内容。

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

如果你和我一样,在刚开始学习的时候,对使用@OneToMany带有 ' mappedBy' 属性的注解的概念感到头疼,这也意味着持有@ManyToOne带有@JoinColumn'的注解的另一边是这个双向的“所有者”关系。

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

此外,mappedBy将类变量的实例名称mCustomer在本例中)作为输入,而不是类类型(例如:客户)或实体名称(例如:客户)。

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

奖励:另外,查看注释的orphanRemoval属性@OneToMany。如果设置为 true,那么如果在双向关系中删除了父级,Hibernate 会自动删除它的子级。