我有两个名为Customer
和的实体类Activity
。
Customer
有customerId
, customerName
,activities
这里activities
持有一组Activity
对应于每个Customer
映射@OneToMany(mappedBy = "customer")
关系。
该类Customer
的定义如下(为了清楚起见,我删除了其他字段和一些 getter 和 setter):
@javax.persistence.Entity
@Table(name = "CUSTOMER")
public class Customer extends Entity {
private String customerId;
private String customerName;
private Set<Activity> activities;
@NaturalId
@Column(name = "CUSTOMER_ID", nullable = false)
public String getCustomerId() {
return customerId;
}
@Column(name = "CUSTOMER_NAME", nullable = false)
public String getCustomerName() {
return customerName;
}
@OneToMany(mappedBy = "customer", fetch = FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
public Set<Activity> getActivities() {
return activities;
}
}
Activity
有activityId
, activityName
,customer
由customerId
with@ManyToOne
关系映射。
Activity
类定义如下:
@javax.persistence.Entity
@Table(name = "ACTIVITY")
public class Activity extends Entity {
private String activityId;
private String activityName;
private Customer customer;
@NaturalId
@Column(name = "ACTIVITY_ID", nullable = false)
public String getActivityId() {
return activityId;
}
@Column(name = "ACTIVITY_NAME", nullable = false)
public String getActivityName() {
return activityName;
}
@ManyToOne
@JoinColumn(name = "CUSTOMER_ID", nullable = false)
public Customer getCustomer() {
return customer;
}
}
在保存一个新的之前Activity
,我将它添加Activity
到activities
集合中Customer
。
我想知道:
- 哪里得救了
activities
?Customer
- 两个实体的更新和删除操作将如何相互影响?
@OneToMany
这是建立和@ManyToOne
建立关系的正确方式吗?- 我可以添加列名
activities
吗?
我是hibernate的初学者,任何指针都会对我很有帮助。
提前致谢。