101

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

我有以下内容:

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

我在这里做错了什么

4

3 回答 3

178

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

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

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

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

于 2010-10-25T02:34:41.343 回答
17

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

如果你和我一样在学习和理解使用@OneToMany带有 ' ' 属性的注释的概念的最初几天,这也意味着持有带有 ' 属性的注释mappedBy的另一方是这个双向的'所有者'关系。@ManyToOne@JoinColumn

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

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

于 2019-05-20T23:57:27.097 回答
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;
}

我从答案中了解到。mappedy="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**;

我给用户模型和通知字段加粗字体。用户模型 mappedBy=" sender " 应该等于通知 List sender;并且 mappedBy=" receiver " 应该等于通知列表 接收者;如果没有,你会得到错误。

于 2020-06-06T15:55:12.050 回答