0

嗨,伙计们,我在尝试将对象数组写入数据库时​​遇到了 Hibernate 问题。基本我有一个从 Web 服务查询构建的对象。这个对象“响应”最多可以包含十个“未付项目”,当我尝试坚持这些时,我的问题就出现了。

实体:

@Entity
@Table(name="TABLE_NAME")
public class AccountDetailsRROutput implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id

    private String payeename;

    private String typeunpd;

    private BigDecimal unpdamt;

    @Column(name="TRANSACTION_ID")
    private long transactionId;

    public AccountDetailsRROutput() {
        super();
    }

    // plus all get/sets
}



        //================================================================
        //  Populate the output for the repeating rows table
        //  which can contain a maximum of 10 unpaid items
        //===============================================================
        AccountDetailsRROutput outputRRTable[] = new AccountDetailsRROutput[response.getLineItems().length];

        LOGGER.debug(METHOD_NAME, "Loop through the line items");
        for (int i = 0; i < response.getLineItems().length; i++) {

            //================================================================
            //  Ensure that we have an item so we don't write an empty row
            //================================================================
            if (response.getLineItems()[i].getTypeunpd() == null || response.getLineItems()[i].getTypeunpd() == "") {
                LOGGER.debug(METHOD_NAME, "No unpaid item entry so break out of the the loop");
                break;
            }
            else {
                LOGGER.debug(METHOD_NAME, "We've got an unpaid item so add the details to the DB");

                outputRRTable[i] = new AccountDetailsRROutput();

                outputRRTable[i].setTransactionId(iTransactionID);
                outputRRTable[i].setTypeunpd(response.getLineItems()[i].getTypeunpd());
                outputRRTable[i].setPayeename(response.getLineItems()[i].getPayeeName());
                outputRRTable[i].setUnpdAmt(response.getLineItems()[i].getUnpdAmt());

                //================================================================
                //  Persist the output list DB object
                //================================================================
                LOGGER.debug(METHOD_NAME, "Persist repeating rows table DB object for line item: " + (i+1));
                em_i.persist(outputRRTable[i]);             
            }
        }
    LOGGER.debug(METHOD_NAME, "Finished persisting repeating rows table DB object");
    em_i.flush();

当我尝试这个时,我收到以下错误:

org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联:

我可以通过将 emi.persist 更改为 emi.merge 来解决这个问题,但它只是将一个元素写入数据库。该表可以有重复记录,没有pk。

4

2 回答 2

2

You probably have more than one item with the same payeename. Try defining another id (for example an id composed of payeename and transactionID).

于 2012-11-14T11:55:24.697 回答
2

As per your hibernate mapping payeename is your primary key which means there can be only one entry per payeename in your database, the exception says NonUniqueObjectException which indicate that you are trying to persist another row with the same primary key.

Solution:

  1. Before inserting, make sure there is no entry in the database with the primary key.

  2. If the primary key already exist instead of insert a new record, do an update.

Hope it helps.

于 2012-11-14T12:01:12.757 回答