1
伊夫加入2-休眠模型子實體對象 第一個表休眠 - 填充在插入
@Entity
@Table(name = "ACTIVITIES")
public class ActivityMO extends ModelBase {
@Column(name = "CA_ID", nullable = false, insertable = true,updatable = true, length = 22, precision = 0)
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "G1")
@SequenceGenerator(name = "G1", sequenceName = "CSM_ACTIVITIES_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "activityId", cascade = {CascadeType.ALL})
@Fetch(FetchMode.JOIN)
List<ActivitiesProductsMO> relatedProducts;
...getters/setters
}
另一表是
@Entity
@Table(name = "ACTIVITIES_PRODUCTS")
public class ActivitiesProductsMO {
@Column(name = "CAP_ID")
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "G1")
@SequenceGenerator(name = "G1", sequenceName = "ACTIVITIES_PRODUCTS_SEQ")
private Long id;
@Column(name = "CAP_ACTIVITY_ID")
private Long activityId;
@Column(name = "CAP_PRODUCT_ID")
private Long productId;
...getters/setters
}
的點是填充爲ActivitiesProductsMO.activityId每分貝記錄與ActivityMO.id值
iee 如果我創建一個活動記錄ID = 555 我會得到activityId爲555的另一個activity_product記錄
我該如何得到這個工作? 謝謝!
請澄清:如果你插入一個新的ActivityMO,你想改變現有的所有ActivitiesProductsMO以指向ActivityMO (通過它的activityId映射字段),還是你想創建一個新的ActivitiesProductsMO,同時保持所有其他的不變?我想它是第二次......但是接下來我的問題是:爲什麼會是一個?您使用OneToMany定義實體,這意味着對於每個ActivityMO,都可以有多個鏈接的ActivityProductsMO。 –
嗨。它不完全是一個,它只是一個例子。如果activityMO帶有20個activityProductMOs列表,則需要創建20個新的ACTIVITY_PRODUCT記錄,並且所有這些記錄都需要具有activityId = activityMO.id –