通过引用未知目标实体属性


85

我在被注释的对象中建立一对多关系时遇到问题。

我有以下内容:

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

我在这里做错了什么

Answers:


148

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

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

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

mappingBy参考指示“在我有一个用于查找配置的集合的东西上查找名为'customer'的bean属性。”


的工作,我曾希望它使用反射使用setter或getter方法,而不是直接使用属性。
boyd4715

@ boyd4715:您可以尝试在getter上移动注释,以查看使用属性访问(与字段访问)时发生的情况。另一方面,的javadocmappedBy拥有关系的字段,所以我不确定这会改变什么。
Pascal Thivent

由于这是帮了我很多
乌萨马·班纳

9

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

如果您在学习的最初阶段像我一样,全神贯注地使用@OneToMany带有' mappedBy'属性的注释的概念,这也意味着持有@ManyToOne带有@JoinColumn'关系。

同样,mappedBy将Class变量的实例名称mCustomer在此示例中)作为输入,而不是Class-Type(ex:Customer)或实体名称(Ex:customer)作为输入。

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


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 =“ sender ”应该等于通知列表 发件人;和mappingBy =“接收者”应该等于通知列表 接收者; 如果没有,您将得到错误。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.