我的用例如下:我使用MySQL和JPA 2,我有两个实体:订单和 LineItem ,它们由OneToMany关系关联。
我希望GUI层能够同时将Order与LineItem一起保存。
请记住,MySQL的 PK自动生成,是LineItem PK的一部分,我不知道如何检索生成的订单PK,以便将其用于同一事务中的LineItem PK。
有人可以帮忙吗?
除了您的反馈之外,这里还有一些来自实体的示例代码(我实际构成了上面的用例/域。我的更复杂,因此您不会看到任何Order或LineItem)。
@Entity
@Table(name = "advertisement")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "status_need_ID", discriminatorType = DiscriminatorType.INTEGER)
@NamedQueries({
@NamedQuery(name = "Advertisement.findAll", query = "SELECT a FROM Advertisement a"),
@NamedQuery(name = "Advertisement.findByAdvertisementID", query = "SELECT a FROM Advertisement a WHERE a.advertisementID = :advertisementID"),
@NamedQuery(name = "Advertisement.findByAdvertisementTitle", query = "SELECT a FROM Advertisement a WHERE a.advertisementTitle = :advertisementTitle"),
@NamedQuery(name = "Advertisement.findByAdvertisementCreationDate", query = "SELECT a FROM Advertisement a WHERE a.advertisementCreationDate = :advertisementCreationDate"),
@NamedQuery(name = "Advertisement.findByStatusneedID", query = "SELECT a FROM Advertisement a WHERE a.statusneedID = :statusneedID")})
public class Advertisement implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "advertisement_ID", nullable = false)
private Integer advertisementID;
@Basic(optional = false)
@Column(name = "advertisement_title", nullable = false, length = 200)
private String advertisementTitle;
@Column(name = "advertisement_creation_date")
@Temporal(TemporalType.TIMESTAMP)
private Date advertisementCreationDate;
@Basic(optional = false)
@Lob
@Column(name = "advertisement_body", nullable = false, length = 2147483647)
private String advertisementBody;
@Column(name = "status_need_ID")
private Integer statusneedID;
@JoinTable(name = "postcode_to_advertisement_join", joinColumns = {
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "postcode_ID", referencedColumnName = "postcode_ID", nullable = false)})
@ManyToMany
private Collection<Postcode> postcodeCollection;
@JoinTable(name = "advertisement_to_duration_join", joinColumns = {
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "duration_ID", referencedColumnName = "duration_ID", nullable = false)})
@ManyToMany
private Collection<Duration> durationCollection;
@JoinTable(name = "full_or_part_time_basis_to_advertisement_join", joinColumns = {
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "full_or_part_time_basis_ID", referencedColumnName = "full_or_part_time_basis_ID", nullable = false)})
@ManyToMany
private Collection<FullOrPartTimeBasis> fullOrPartTimeBasisCollection;
@JoinTable(name = "advertisement_to_child_care_type_join", joinColumns = {
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "child_care_type_ID", referencedColumnName = "child_care_type_ID", nullable = false)})
@ManyToMany
private Collection<ChildCareType> childCareTypeCollection;
@JoinTable(name = "advertisement_to_school_holiday_join", joinColumns = {
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "school_holiday_ID", referencedColumnName = "school_holiday_ID", nullable = false)})
@ManyToMany
private Collection<SchoolHoliday> schoolHolidayCollection;
@JoinTable(name = "employment_contract_to_advertisement_join", joinColumns = {
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "employment_contract_ID", referencedColumnName = "employment_contract_ID", nullable = false)})
@ManyToMany
private Collection<EmploymentContract> employmentContractCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "advertisement")
private Collection<AdvertisementToTimeSlotToDayJoin> advertisementToTimeSlotToDayJoinCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "advertisementID")
private Collection<Salary> salaryCollection;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "advertisement")
private ParentToParentAdvertisement parentToParentAdvertisement;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "advertisement")
private ParentToChildMinderAdvertisement parentToChildMinderAdvertisement;
@JoinColumn(name = "child_care_location_ID", referencedColumnName = "child_care_location_ID", nullable = false)
@ManyToOne(optional = false)
private ChildCareLocation childcarelocationID;
@JoinColumn(name = "account_ID", referencedColumnName = "account_ID", nullable = false)
@ManyToOne(optional = false)
private Account accountID;
@Entity
@Table(name = "advertisement_to_time_slot_to_day_join",schema="bignibou")
@NamedQueries({
@NamedQuery(name = "AdvertisementToTimeSlotToDayJoin.findAll", query = "SELECT a FROM AdvertisementToTimeSlotToDayJoin a"),
@NamedQuery(name = "AdvertisementToTimeSlotToDayJoin.findByDayID", query = "SELECT a FROM AdvertisementToTimeSlotToDayJoin a WHERE a.advertisementToTimeSlotToDayJoinPK.dayID = :dayID"),
@NamedQuery(name = "AdvertisementToTimeSlotToDayJoin.findByTimeslotID", query = "SELECT a FROM AdvertisementToTimeSlotToDayJoin a WHERE a.advertisementToTimeSlotToDayJoinPK.timeslotID = :timeslotID"),
@NamedQuery(name = "AdvertisementToTimeSlotToDayJoin.findByAdvertisementID", query = "SELECT a FROM AdvertisementToTimeSlotToDayJoin a WHERE a.advertisementToTimeSlotToDayJoinPK.advertisementID = :advertisementID")})
public class AdvertisementToTimeSlotToDayJoin implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected AdvertisementToTimeSlotToDayJoinPK advertisementToTimeSlotToDayJoinPK;
@JoinColumn(name = "time_slot_ID", referencedColumnName = "time_slot_ID", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private TimeSlot timeSlot;
@JoinColumn(name = "day_ID", referencedColumnName = "day_ID", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private Day day;
@JoinColumn(name = "advertisement_ID", referencedColumnName = "advertisement_ID", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private Advertisement advertisement;
答案 0 :(得分:2)
这里的基本想法是,您必须分两个阶段保存实体。当然,您可以在同一笔交易中完成。首先保存订单,不保存LineItem,然后检索订单中的PK,然后在LineItem上设置它,然后保存LineItem(s)。我想这是OneToMany注释的CascadeType属性,它将告诉JPA在两个不同的阶段执行此操作,即在保存Order时不保存LineItems。像这样:
@Entity
public class Order {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
long id;
@OneToMany(cascade={})
Set<LineItem> lineItems;
}
或者你可以在保存Order时将lineItems设置为空,然后检索PK,为lineItems设置lineItems和PK,然后再次保存Order。
如果您的数据库设计是最终的,并且无法更改,请忽略此部分答案。
这是我的私人意见(基于多年的数据库设计和维护)。那么,首先,你是否有充分的理由在LineItem中使用复合PK而不是使用好的旧surrogate primary key?
因为如果不这样做,那么你应该在LineItem中有一个代理主键。否则,您将继续遇到各种问题(例如,如果您碰巧需要迁移数据)
编辑我的建议: