我的用例如下:我使用MySQL和JPA 2,并且有两个实体:Order和LineItem,它们通过OneToMany关系关联。
我想让GUI层同时将Order和LineItem保留在一起。
记住Order PK是由MySQL自动生成的,并且是LineItem PK的一部分,我不知道如何检索生成的Order 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;
最佳答案
这里的基本思想是必须分两个阶段保存实体。当然,您可以在同一笔交易中完成此操作。首先,您保存订单而不保存LineItem(s),然后检索订单的PK,然后将其设置在LineItem(s)上,然后保存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中具有代理主键。否则,您将不断遇到各种问题(例如,如果您碰巧不得不迁移数据)
编辑我的建议:
在数据库中:在表中使用auto_increment PK,如下例所示:http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
在JPA映射中:转到本文的第一个选项(AUTO):http://www.objectdb.com/java/jpa/entity/generated
关于mysql - Mysql自动生成的PK用于另一个表和JPA的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8897300/