mappedBy引用未知的目标实体属性

时间:2010-10-25 02:14:49

标签: java hibernate orm hibernate-annotations

我在注释对象中设置一对多关系时遇到问题。

我有以下内容:

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

然后这个

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

和这个

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

我在这里做错了什么

3 个答案:

答案 0 :(得分:106)

mappedBy属性引用customer,而属性为mCustomer,因此出现错误消息。因此,要么将映射更改为:

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

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

mappedBy引用指示“在我找到配置的东西上查看名为'customer'的bean属性。”

答案 1 :(得分:0)

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

如果在学习的最初阶段像我一样,并且将头绪与使用 @OneToMany 批注和' mappedBy '属性一起使用的概念一样,它也会表示另一边带有 @JoinColumn @ManyToOne 注释是此双向关系的“所有者”。

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

奖金: 另外,查看@OneToMany批注的 orphanRemoval 属性。如果将其设置为true,则如果以双向关系删除了父级,则Hibernate会自动删除其子级。

答案 2 :(得分:0)

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;
}

我从答案中了解的内容。 mappingy =“ sender”值在通知模型中应该相同。我给你举个例子。

用户模型:

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

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

通知模型:

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

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

我为用户模型和通知字段提供了粗体。用户模型mappingBy =“ 发件人”应等于通知列表发件人;并且mappingBy =“ receiver ”应该等于通知列表 receiver ;如果没有,您将得到错误。