我有一個表有2個主鍵(所以他們兩個的組合應該是唯一的)。該模式是這樣的:多主鍵表 - 休眠NonUniqueObjectException
TABLE="INVOICE_LOGS"
INVOICE_NUMBER PK non-null
INVOICE_TYPE PK non-null
AMOUNT
當我嘗試一次堅持多條記錄,我得到這個休眠例外:
產生的原因: javax.persistence.PersistenceException: org.hibernate作爲.NonUniqueObjectException: 具有相同 標識符值不同的物體已經與所述會話相關聯 : [... InvoiceLog#110105269]
所以,如果我使用下面的代碼,我得到了上面的休眠異常。如果我只保留一條記錄,但是如果我試圖保留像我正在嘗試做的那樣的集合,就會失敗。
List<InvoiceLog> logs = getLogs();
for(OvercounterLogDO log: logs) {
persist(log);
}
public void persist(final Object entity) {
entityManager.persist(entity);
entityManager.flush();
}
Hibernate映射類低於:
@Component(InvoiceLog.BEAN_NAME)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Entity
@Table(name = "INVOICE_LOGS", uniqueConstraints = {@UniqueConstraint(columnNames = "INVOICE_NUMBER"),@UniqueConstraint(columnNames = "INVOICE_TYPE")})
public class InvoiceLog implements java.io.Serializable {
private static final long serialVersionUID = -7576525197897271909L;
protected static final String BEAN_NAME = "invoiceLog";
private long invoiceNumber;
private String invoiceType;
private Double amount;
public InvoiceLog(){}
public InvoiceLog(Integer invoiceNumber, String invoiceType,
Double amount) {
super();
this.invoiceNumber = invoiceNumber;
this.invoiceType = invoiceType;
this.amount = amount;
}
@Id
@Column(name = "INVOICE_NUMBER", unique = true, nullable = false, precision = 10, scale = 0)
public long getInvoiceNumber() {
return invoiceNumber;
}
public void setInvoiceNumber(long invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
// @Id
@Column(name = "INVOICE_TYPE", nullable = false, length = 4)
public String getInvoiceType() {
return invoiceType;
}
public void setInvoiceType(String invoiceType) {
this.invoiceType = invoiceType;
}
@Column(name = "AMOUNT", nullable = false, length = 12)
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
}