Java JPA OneToMany具有身份生成的父ID持久化错误
请大家给点建议 我在JPA中坚持一对一的关系时遇到了一个问题,请看这个例子 父CalsJava JPA OneToMany具有身份生成的父ID持久化错误,java,jpa,eclipselink,derby,Java,Jpa,Eclipselink,Derby,请大家给点建议 我在JPA中坚持一对一的关系时遇到了一个问题,请看这个例子 父Cals @Entity @Table(name="SALES_INVOICES") public class SalesInvoice { @Id @Column(name="INV_ID") @GeneratedValue(strategy=GenerationType.IDENTITY) private long invoi
@Entity
@Table(name="SALES_INVOICES")
public class SalesInvoice {
@Id
@Column(name="INV_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long invoiceID;
@Column(name="INV_NO", nullable=false)
private long invoiceNumber;
@Column(name="DOC_TYPE")
private String docType;
@Column(name="INV_TOTAL")
private BigDecimal totalInvoice;
@Column(name="NOTES")
private String notes;
@Column(name="STORE_ID")
private int storeID;
@Column(name="INV_DATE")
private Date invoiceDate;
@Column(name="CUST_ID",nullable=true)
private int cusomerID;
@Column(name="OPER_TYPE")
private String operType;
@Column(name="TECH_ID", nullable=true)
private int techID;
@ManyToOne
@PrimaryKeyJoinColumn(name="STORE_ID", referencedColumnName="STORE_ID")
private Store store;
@ManyToOne
@PrimaryKeyJoinColumn(name="CUST_ID", referencedColumnName="CUST_ID")
private Customer customer;
@OneToMany(mappedBy="invoice",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="INV_ID")
private List<InvoiceLine> lines;
@ManyToOne
@PrimaryKeyJoinColumn(name="TECH_ID",referencedColumnName="TECH_ID")
private Technician technician;
/**
* @return the operTypeID
*/
public String getOperType() {
return operType;
}
/**
* @param operTypeID the operTypeID to set
*/
public void setOperType(String operType) {
this.operType = operType;
}
/**
* @return the techID
*/
public int getTechID() {
return techID;
}
/**
* @param techID the techID to set
*/
public void setTechID(int techID) {
this.techID = techID;
}
/**
* @return the lines
*/
public List<InvoiceLine> getLines() {
return lines;
}
/**
* @param lines the lines to set
*/
public void setLines(List<InvoiceLine> lines) {
this.lines = lines;
}
/**
* @return the technician
*/
public Technician getTechnician() {
return technician;
}
/**
* @param technician the technician to set
*/
public void setTechnician(Technician technician) {
this.technician = technician;
}
/**
* @return the cusomerID
*/
public int getCusomerID() {
return cusomerID;
}
/**
* @param cusomerID the cusomerID to set
*/
public void setCusomerID(int cusomerID) {
this.cusomerID = cusomerID;
}
/**
* @return the store
*/
public Store getStore() {
return store;
}
/**
* @param store the store to set
*/
public void setStore(Store store) {
this.store = store;
}
/**
* @return the customer
*/
public Customer getCustomer() {
return customer;
}
/**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
public SalesInvoice(){}
/**
* @return the invoiceID
*/
public long getInvoiceID() {
return invoiceID;
}
/**
* @param invoiceID the invoiceID to set
*/
public void setInvoiceID(long invoiceID) {
this.invoiceID = invoiceID;
}
/**
* @return the invoiceNumber
*/
public long getInvoiceNumber() {
return invoiceNumber;
}
/**
* @param invoiceNumber the invoiceNumber to set
*/
public void setInvoiceNumber(long invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
/**
* @return the docType
*/
public String getDocType() {
return docType;
}
/**
* @param docType the docType to set
*/
public void setDocType(String docType) {
this.docType = docType;
}
/**
* @return the totalInvoice
*/
public BigDecimal getTotalInvoice() {
return totalInvoice;
}
/**
* @param totalInvoice the totalInvoice to set
*/
public void setTotalInvoice(BigDecimal totalInvoice) {
this.totalInvoice = totalInvoice;
}
/**
* @return the notes
*/
public String getNotes() {
return notes;
}
/**
* @param notes the notes to set
*/
public void setNotes(String notes) {
this.notes = notes;
}
/**
* @return the storeID
*/
public int getStoreID() {
return storeID;
}
/**
* @param storeID the storeID to set
*/
public void setStoreID(int storeID) {
this.storeID = storeID;
}
/**
* @return the invoiceDate
*/
public Date getInvoiceDate() {
return invoiceDate;
}
/**
* @param invoiceDate the invoiceDate to set
*/
public void setInvoiceDate(Date invoiceDate) {
this.invoiceDate = invoiceDate;
}
}
//////////////////////////////////////////////
HERE IS THE CHILD CLASS....
@Entity
@Table(name="INV_LINES")
public class InvoiceLine {
@Id
@Column(name="LINE_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long lineID;
@Column(name="INV_ID")
private long invoiceID;
@Column(name="STOCK_ITEM")
private String stockItem;
@Column(name="QTY")
private int quantity;
@Column(name="TOTAL_PRICE")
private BigDecimal totalPrice;
@Column(name="NOTES")
private String notes;
@Column(name="WARRANTY")
private boolean warranted;
@Column(name="DAYS_WARRANTY")
private int warrantyDays;
@ManyToOne
@PrimaryKeyJoinColumn(name="STOCK_ITEM",referencedColumnName="ITEM_ID")
private StockItem item;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn(name="INV_ID", referencedColumnName="INV_ID")
private SalesInvoice invoice;
/**
* @return the invoiceID
*/
public long getInvoiceID() {
return invoiceID;
}
/**
* @param invoiceID the invoiceID to set
*/
public void setInvoiceID(long invoiceID) {
this.invoiceID = invoiceID;
}
/**
* @return the invoice
*/
public SalesInvoice getInvoice() {
return invoice;
}
/**
* @param invoice the invoice to set
*/
public void setInvoice(SalesInvoice invoice) {
this.invoice = invoice;
this.invoiceID = invoice.getInvoiceID();
}
public InvoiceLine(){}
/**
* @return the lineID
*/
public long getLineID() {
return lineID;
}
/**
* @param lineID the lineID to set
*/
public void setLineID(long lineID) {
this.lineID = lineID;
}
/**
* @return the stockItem
*/
public String getStockItem() {
return stockItem;
}
/**
* @param stockItem the stockItem to set
*/
public void setStockItem(String stockItem) {
this.stockItem = stockItem;
}
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/**
* @return the totalPrice
*/
public BigDecimal getTotalPrice() {
return totalPrice;
}
/**
* @param totalPrice the totalPrice to set
*/
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
/**
* @return the notes
*/
public String getNotes() {
return notes;
}
/**
* @param notes the notes to set
*/
public void setNotes(String notes) {
this.notes = notes;
}
/**
* @return the warranted
*/
public boolean isWarranted() {
return warranted;
}
/**
* @param warranted the warranted to set
*/
public void setWarranted(boolean warranted) {
this.warranted = warranted;
}
/**
* @return the warrantyDays
*/
public int getWarrantyDays() {
return warrantyDays;
}
/**
* @param warrantyDays the warrantyDays to set
*/
public void setWarrantyDays(int warrantyDays) {
this.warrantyDays = warrantyDays;
}
/**
* @return the item
*/
public StockItem getItem() {
return item;
}
/**
* @param item the item to set
*/
public void setItem(StockItem item) {
this.item = item;
}
}
谢谢大家您的地图不正确;当您应该使用@JoinColumn时,您正在OneToOne和ManyToOne映射上使用@PrimaryKeyJoinColumn;当OneToMany映射应该指定它由其他关系映射时,您正在OneToMany映射上使用@JoinColumn 在SalesInvoice中,请尝试:
@ManyToOne
@JoinColumn(name="STORE_ID", referencedColumnName="STORE_ID")
private Store store;
@ManyToOne
@JoinColumn(name="CUST_ID", referencedColumnName="CUST_ID")
private Customer customer;
@OneToMany(mappedBy="invoice",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private List<InvoiceLine> lines;
@ManyToOne
@JoinColumn(name="TECH_ID",referencedColumnName="TECH_ID")
private Technician technician;
这就是如何使SalesInvoice-InvoiceLine成为双向关系的方法。不要忘记,应用程序负责保持双向关系的双方彼此和数据库同步。JPA不会为您这样做,缓存将被损坏
您还需要删除长invoiceID映射,将其设置为只读或设置值,因为这是在您以前的实体设置中用于设置“INV_ID”字段的内容-这可能是您使用@PrimaryKeyJoinColumn的原因,因为提供程序会抱怨您有多个可写映射。评估其用途,如果不需要,请将其删除,否则我将使用以下命令将其设置为只读:
@Column(name="INV_ID", insertable=false, updatable=false)
private long invoiceID;
因为这将允许您将属性保留在实体中,但是让JPA使用发票关系中的值来设置它 请发布您的服务代码,您如何持久化实体?
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="INV_ID", referencedColumnName="INV_ID")
private SalesInvoice invoice;
@Column(name="INV_ID", insertable=false, updatable=false)
private long invoiceID;