简体   繁体   中英

Multiple Primary Keys Table - Hibernate NonUniqueObjectException

I have a table with 2 primary keys (so the combination of both of them should be unique). The schema is like this:

TABLE="INVOICE_LOGS"
INVOICE_NUMBER  PK  non-null
INVOICE_TYPE    PK  non-null
AMOUNT  

When I try to persist multiple records at a time, I get this hibernate exception:

Caused by: javax.persistence.PersistenceException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [...InvoiceLog#110105269]

So if I use the following code below, I get the hibernate exception above. It works fine if I only persist one record but fails if I try to persist a collection like what I'm trying to do.

    List<InvoiceLog> logs = getLogs();
    for(OvercounterLogDO log: logs) {
        persist(log);
    }

    public void persist(final Object entity) {
        entityManager.persist(entity);
        entityManager.flush();
    }

The hibernate mapping class is below:

@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;
    }

}

Your current mapping has a single Id, invoiceNumber, not a compound key. As such, it'll demand that the invoiceNumber be unique, not the combination you want. That's what the exception is telling you, it's noticing you're trying to save a second record with the same Id value.

You need to map the two fields as a composite key. It looks from your commenting out the second @Id that you were going in that direction at some point. Here's the documentation on methods for mapping the composite key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM