我在带注释的对象中设置一对多关系时遇到问题。
我有以下内容:
@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;
我在这里做错了什么
mappedBy
属性在属性为 mCustomer
时引用 customer
,因此出现错误消息。因此,要么将您的映射更改为:
/** The collection of stores. */
@OneToMany(mappedBy = "mCustomer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Collection<Store> stores;
或者将实体属性更改为 customer
(这就是我要做的)。
mappedBy 引用指示“在我收集的东西上查看名为 'customer' 的 bean 属性以查找配置。”
我知道 @Pascal Thivent 的答案已经解决了这个问题。我想在他对可能正在浏览此线程的其他人的回答中添加更多内容。
如果您像我一样在刚开始学习并理解使用带有“mappedBy
”属性的 @OneToMany
注释的概念时,这也意味着另一方持有 @ManyToOne
注释和 { 4} 是这种双向关系的“所有者”。
此外,mappedBy
将 Class 变量的 实例名称(在本例中为 mCustomer
)作为输入,而不是 Class-Type(例如:Customer)或实体名称(例如:客户)。
奖励:另外,查看 @OneToMany
注释的 orphanRemoval
属性。如果设置为 true,那么如果在双向关系中删除父级,Hibernate 会自动删除它的子级。
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" 应该等于通知列表发送者;而且 mappedBy="receiver" 应该等于通知列表接收者;如果没有,你会得到错误。
mappedBy
的 javadoc 说 拥有关系的字段,所以我不确定这会改变什么。